package de.nanoleaf.server.test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.github.rowak.nanoleafapi.Aurora;
import io.github.rowak.nanoleafapi.Canvas;
import io.github.rowak.nanoleafapi.Color;
import io.github.rowak.nanoleafapi.NanoleafDevice;
import io.github.rowak.nanoleafapi.Panel;
import io.github.rowak.nanoleafapi.event.DetailedTouchEvent;
import io.github.rowak.nanoleafapi.event.NanoleafTouchEventListener;
import io.github.rowak.nanoleafapi.event.TouchEvent;
import io.github.rowak.nanoleafapi.util.NanoleafDeviceMeta;
import io.github.rowak.nanoleafapi.util.NanoleafSetup;

/**
 * https://github.com/amybytes/nanoleaf-api
 * 
 */

public class NanoleafServer {

	List<NanoleafDeviceMeta> m_nanoleafDeviceList = null;
	public List<NanoleafDeviceMeta> getNanoleafDeviceList() { return m_nanoleafDeviceList; }

	public NanoleafServer() {
		
		String host = "Canvas-72FA.fritz.box";
		int port = 16021;
		String apiLevel = "v1";
		String accessToken = "LAG28BQWrXK2p79NbHMQsvcYYCN1TcD4";
		
		Canvas nanoleafCanvas = null;
		
		Set<Integer> lastIDSet = new HashSet<Integer>();
		
		try {
			//nanoleafCanvas = new Canvas(host, port, accessToken);
			

	        // Verbinde mit dem Gerät
	        NanoleafDevice device = NanoleafDevice.createDevice(host, port, accessToken);

//	        List<Panel> panelList = device.getPanels();
//	        
//	        for (Panel panel: panelList) {
//	        	System.out.println("Panel: " + panel.getId());
//	        }
	        device.setColor(Color.BLACK);
	        
	        device.enableTouchEventStreaming(port); // You need to specify a port to listen on
	        device.registerTouchEventStreamingListener(new NanoleafTouchEventListener() {
	            @Override
	            public void onOpen() {
	                // Called when the listener is created
	            }

	            @Override
	            public void onClosed() {
	                // Called when the listener is closed
	            }

	            @Override
	            public void onEvent(DetailedTouchEvent[] events) {
	                // Called when a touch event occurs
	            	//System.out.println("Events: " + events.length);
	            	
	            	DetailedTouchEvent event = events[0];
	            	
	            	Integer lastID = event.getPanelId();
	            	
	            	
	            	
	            	
	            	//TouchEvent touch = event.getTouchType();
	            	
	            	switch (event.getTouchType()) {
					case TouchEvent.DOUBLE_TAP: 
						//System.out.println("DOUBLE_TAP");				
						break;
						
					case TouchEvent.SINGLE_TAP:
						//System.out.println("SINGLE_TAP");
						if (lastIDSet.contains(lastID)) {
		            		return;
		            	} else {
		            		lastIDSet.clear();
		            		lastIDSet.add(lastID);
		            		
		            		try {
		            			
		            			List<Panel> panels = device.getPanels();
		            			device.enableExternalStreaming();    
		            			
		            			
		            			// enable external streaming
//		            			for (int i = 0; i < 4; i++) {
//		            				device.setPanelExternalStreaming(panels.get(i), "#FF00FF", 1);  // set a few panels to purple
//		            			}
		            			
		            			device.setPanelExternalStreaming(lastID, Color.RED, 1);
		            			
		            			
//		            			int panelID = event.getInitialPanelId();		             
//		            			
//		            			//device.setPanelColor(panelID, "#ff0000", 2);
//		            			
//		            			device.setPanelExternalStreaming(panelID, Color.RED, 1);
		            			
		            		} catch (Exception e) {
								e.printStackTrace();
							}
		            		
		            		
		            		
		            		System.out.println("ID: " + event.getPanelId());
		            	}
						
						
						break;
						
					case TouchEvent.HOLD:
						//System.out.println("HOLD");
						break;
						
					case TouchEvent.UNKNOWN_ATTRIBUTE:
						//System.out.println("UNKNOWN_ATTRIBUTE");
						break;
						
						
					default:
						//System.out.println("Unexpected value: " + event.getTouchType());
					} 
	            	
	            	
	            	
	            	
	            	
	            }
	        });
	        
	        
	        
//	        device.registerEventListener(new NanoleafEventListener() {
//	        @Override
//	        public void onOpen() {
//	            // Called when the listener is created
//	        	System.out.println("open");
//	        }
//
//	        @Override
//	        public void onClosed() {
//	            // Called when the listener is closed
//	        	System.out.println("close");
//	        }
//
//	        @Override
//	        public void onEvent(Event[] events) {
//	            // Called when an event occurs (~1-2 second delay)
//	        	System.out.println("Event: " + events.length);
//	        	Event event = events[0];
//
//	        	System.out.println("Event: " + event.toJSON());
//	        
//	        }
//	    }, true, true, true, true);
	        
	        
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		
		
		
	}
	
	public void aurora() {
		Aurora aurora = null;
		try {
			
			String host = "Canvas-72FA.fritz.box";
			int port = 16021;
			String accessToken = "LAG28BQWrXK2p79NbHMQsvcYYCN1TcD4";
			
			aurora = new Aurora(host, port, accessToken);
			
			boolean isOn = aurora.getOn();
			
			if (isOn) {
				System.out.println("Nanoleaf ist an!!!");
				
				int brightness = aurora.getBrightness();
				
				System.out.println("Helligkeit: " + brightness);
				
				List<Panel> panelList = aurora.getPanels();
				
				for (Iterator iterator = panelList.iterator(); iterator.hasNext();) {
					Panel panel = (Panel) iterator.next();
					
					System.out.println("Panel ID: " + panel.getId());
				}
				
				
				
			} else {
				System.out.println("Nanoleaf ist off!!!");
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void checkAdressToken() {
		
		int timeout = 2000;
		
		/*
		 * 192.168.178.138 Nanoleaf 4D
		 * 
		 * 192.168.178.141 Nanoleaf Canvas P17
		 * Canvas-72FA.fritz.box
		 * 
		 * Nano Devices: 1
		 * Host: Canvas-72FA.fritz.box
		 * DevID: 192.168.178.141
		 * Port: 16021
		 * accessToken: LAG28BQWrXK2p79NbHMQsvcYYCN1TcD4
		 * 
		 * 
		 */
		
		try {
			m_nanoleafDeviceList = NanoleafSetup.findNanoleafDevices(timeout);
			
			System.out.println("Nano Devices: " + m_nanoleafDeviceList.size());
			
			NanoleafDeviceMeta meta = null;
			
			if (getNanoleafDeviceList().size() > 0) {
				meta = getNanoleafDeviceList().get(0);
				System.out.println("Host: " + meta.getHostName());
				System.out.println("DevID: " + meta.getDeviceId());
				System.out.println("Port: " + meta.getPort());
				
				String accessToken = NanoleafSetup.createAccessToken(meta.getHostName(), meta.getPort());
				
				System.out.println("accessToken: " + accessToken);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	public static void main(String[] args) {
		new NanoleafServer();

	}

}
