package de.games.nanoleaf.server;

import java.util.Iterator;
import java.util.List;

import io.github.rowak.nanoleafapi.Aurora;
import io.github.rowak.nanoleafapi.Panel;
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";
		

		Aurora aurora = null;
		try {
			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();

	}

}
