package SCHUNK.EndOfArm.impl;

import com.ur.urcap.api.contribution.ProgramNodeContribution;
import com.ur.urcap.api.domain.URCapAPI;
import com.ur.urcap.api.domain.data.DataModel;
import com.ur.urcap.api.domain.io.DigitalIO;
import com.ur.urcap.api.domain.script.ScriptWriter;
import com.ur.urcap.api.ui.annotation.Img;
import com.ur.urcap.api.ui.annotation.Input;
import com.ur.urcap.api.ui.annotation.Label;
import com.ur.urcap.api.ui.component.ImgComponent;
import com.ur.urcap.api.ui.component.InputButton;
import com.ur.urcap.api.ui.component.InputEvent;
import com.ur.urcap.api.ui.component.InputTextField;
import com.ur.urcap.api.ui.component.LabelComponent;

import java.awt.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class GrpCloseProgramNodeContribution implements ProgramNodeContribution {
	private eoa_Language.TextResource getTextResource() {
		return eoa_Language.LanguagePack.getInstance().getTextResource();
	}
	
	
	private final DataModel model;
	private final URCapAPI api;
	
	private static final String btnClose_KEY = "buttonClose_KEY";
	private static final String btnOpen_KEY = "buttonOpen_KEY";

	private static final String LogoImg = "LogoImg";
	
	private static final String lbl_Open_Close = "lbl_Open_Close";
	private static final String lbl_DM = "lbl_DM";	
	
	@Label (id = lbl_Open_Close)
	private LabelComponent lbl_OpenClose;
	@Label (id = lbl_DM)
	private LabelComponent lbl_DirectMovement;
	
	@Input (id = btnClose_KEY)
	private InputButton btnClose;
	@Input (id = btnOpen_KEY)
	private InputButton btnOpen;
	
	@Img(id = LogoImg)
	public ImgComponent ImgLogo;
	


	public GrpCloseProgramNodeContribution(URCapAPI api, DataModel model) {
		this.api = api;
		this.model = model;
		
	
		}
	


	@Input (id = btnClose_KEY)
	private void onClosePress(InputEvent event){
		if (event.getEventType()==InputEvent.EventType.ON_PRESSED){
			
			get_DOOpen().setValue(false);
			get_DOClose().setValue(false);
			try {
				Thread.sleep(15);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			get_DOOpen().setValue(false);
			get_DOClose().setValue(true);
			
		}
	}
	@Input (id = btnOpen_KEY)
	private void onOpenPress(InputEvent event){
		if (event.getEventType()==InputEvent.EventType.ON_PRESSED){
			
			get_DOOpen().setValue(false);
			get_DOClose().setValue(false);
			try {
				Thread.sleep(15);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			get_DOClose().setValue(false);
			get_DOOpen().setValue(true);
						
		}
	}


	@Override
	public void openView() {
		lbl_OpenClose.setText(getTextResource().get_OpenClose());
		lbl_DirectMovement.setText(getTextResource().get_DirectMovement());
		btnOpen.setText(getTextResource().get_btn_Open());
		btnClose.setText(getTextResource().get_btn_Close());
		
		try{
			ImgLogo.setImage(ImageIO.read(getClass().getResource(getInstallation().getTextResource().get_Path_To_Logo_Image())));		
		}catch  (java.io.IOException e){
			e.printStackTrace();
		}
	}

	@Override
	public void closeView() {
	}

	@Override
	public String getTitle() {
		return getTextResource().get_Title_Grp_Close();	
	}

	@Override
	public boolean isDefined() {
		return true;
	}

	@Override
	public void generateScript(ScriptWriter writer) {

		writer.appendLine("EoaGrpClose()");


	}
	
	private DigitalIO getDigitalIO(String defaultName){
		Collection<DigitalIO> IOcollection = api.getIOs().getIOs(DigitalIO.class);
		int IO_count = IOcollection.size();
		if(IO_count > 0){
			Iterator<DigitalIO> IO_itr = IOcollection.iterator();
			while(IO_itr.hasNext()){
				DigitalIO thisIO = IO_itr.next();
				String thisDefaultName = thisIO.getDefaultName();
				System.out.println("Found an IO named "+thisDefaultName);
				if(thisDefaultName.equals(defaultName)){
					return thisIO;
				}
			}
		}
		return null;
	}
	
	public DigitalIO get_DOClose(){
		String DO_Close = getInstallation().get_DO_Close();
		System.out.println( DO_Close);
		return getDigitalIO(DO_Close);
	}
	public DigitalIO get_DOOpen(){
		String DO_Open = getInstallation().get_DO_Open();
		System.out.println( DO_Open);
		return getDigitalIO(DO_Open);
	}

	private GrpInstallationNodeContribution getInstallation() {
		return api.getInstallationNode(GrpInstallationNodeContribution.class);
	}	
}
