package org.kne.cloud.klalb.uitool;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.NetworkInterface;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;

import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import org.kne.cloud.network.klalb.ui.UIEnv;
import java.awt.FlowLayout;

public abstract class ListSettingItem<T> extends SettingItem {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected JList<T> list;
	
	private Consumer<T> storeRecall;
	public JList<T> getList() {
		return list;
	}
	public ListSettingItem(String text,int w,int h,XDefaultListModel<T>lm) {
		this(text,UIEnv.getFont().deriveFont((float) 15.0),w,h,lm);
	}
/**
	 * @wbp.parser.constructor
	 */
	public ListSettingItem(String text, Font deriveFont,int w,int h,XDefaultListModel<T> lm) {
		super(text,deriveFont,w,h);
		list = new JList<T>(lm);
		JScrollPane js=new JScrollPane(list);
		js.getViewport().setOpaque(false);
		js.setOpaque(false);
		js.setBackground(new Color(0,0,0,0));
		getPanel().add(js, BorderLayout.CENTER);
		list.setBorder(null);
		list.setOpaque(false);
		list.setBackground(new Color(0,0,0,0));
		label.setForeground(Color.GRAY);
		
		Dimension dss=new Dimension(25,25);
		JButton jbt3x=new JButton();
		jbt3x.setToolTipText(UIEnv.getRsb().getString("add")); 
		try {
			jbt3x.setIcon(new ImageIcon(ImageIO .read(ListSettingItem.class.getResourceAsStream("/assets/add.png")).getScaledInstance(dss.width-2, dss.height-2, Image.SCALE_SMOOTH)));
		} catch (IOException e) {
			e.printStackTrace();
		}catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		jbt3x.setSize(dss);
		jbt3x.setPreferredSize(dss);
		jbt3x.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			List<T> x=createEmpty();
			if(x!=null&&!x.isEmpty())
			runEdit(x.get(0));
			}
		});
		JButton jbedit=new JButton();
		jbedit.setToolTipText(UIEnv.getRsb().getString("edit"));
		try {
			jbedit.setIcon(new ImageIcon(ImageIO .read(ListSettingItem.class.getResourceAsStream("/assets/edit-fill.png")).getScaledInstance(dss.width-2, dss.height-2, Image.SCALE_SMOOTH)));
		} catch (IOException e) {
			e.printStackTrace();
		}catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		jbedit.setSize(dss);
		jbedit.setPreferredSize(dss);
		jbedit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				T val=list.getSelectedValue();
				if(val!=null)
				runEdit(val);
			}
		});
		
		JButton jbt4x=new JButton();
		jbt4x.setToolTipText(UIEnv.getRsb().getString("remove"));
		try {
			jbt4x.setIcon(new ImageIcon(ImageIO .read(ListSettingItem.class.getResourceAsStream("/assets/delete-bin-fill.png")).getScaledInstance(dss.width-2, dss.height-2, Image.SCALE_SMOOTH)));
		} catch (IOException e) {
			e.printStackTrace();
		}catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		jbt4x.setSize(dss);
		jbt4x.setPreferredSize(dss);
		jbt4x.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int v=list.getSelectedIndex();
				if(v>=0) {
				lm.remove(v);
				if(storeRecall!=null)
					storeRecall.accept(list.getSelectedValue());
				}
			}
		});
		JPanel jp=new JPanel();
		jp.setPreferredSize(new Dimension(30, h));
		FlowLayout flowLayout = (FlowLayout) jp.getLayout();
		flowLayout.setAlignment(FlowLayout.TRAILING);
		jp.setOpaque(false);
		getPanel().add(jp,BorderLayout.EAST);
		jp.add(jbt3x);
		jp.add(jbedit);
		jp.add(jbt4x);
	}
	abstract protected void runEdit(T val) ;
abstract protected List<T> createEmpty() ;
public void doAdd(T v) {
	boolean b=true;
	for (int i = 0; i < list.getModel().getSize(); i++) {
		if(list.getModel().getElementAt(i)==v) {
			b=false;
				break;
		}
	}
	if(b)
		((XDefaultListModel<T>) list.getModel()).addElement(v);
	repaint();
		if(storeRecall!=null)
			storeRecall.accept(list.getSelectedValue());
}
public Consumer<T> getStoreRecall() {
	return storeRecall;
}
public void setStoreRecall(Consumer<T> storeRecall) {
	this.storeRecall = storeRecall;
}

}
