package de.ctdo.crashtest.domotics;

import de.ctdo.crashtest.log.Logger;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

public class BuntiClient implements IBuntiClient {
    private final String baseAddress;
    //private final HttpClient client = new DefaultHttpClient();

    public BuntiClient(String server, int port) {
        baseAddress = "http://" + server + ":" +  port + "/";
    }

    @Override
    public void setPar56(final int red, final int green, final int blue) {

        System.out.println("BuntiClient: setPar56 " + red + "," + green + "," + blue);

        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(baseAddress + "/control/devices");
                    post.addHeader("Content-Type", "application/json");

                    StringBuilder jsonString = new StringBuilder();

                    jsonString.append("{ \"timeStamp\": 0, \"updates\": [ ");

                    for(int i = 0; i< 4; i++) {
                        jsonString.append(" {\"deviceId\": ");
                        jsonString.append(i);
                        jsonString.append(", \"options\": { \"red\": ");
                        jsonString.append(red);
                        jsonString.append(", \"green\": ");
                        jsonString.append(green);
                        jsonString.append(", \"blue\": ");
                        jsonString.append(blue);
                        jsonString.append(" } } ");
                        if(i!=3) jsonString.append(",");
                    }

                    jsonString.append("] }");

                    StringEntity entity = new StringEntity( jsonString.toString(), "UTF-8");
                    post.setEntity(entity);

                    client.execute(post);
                    post.abort();

                } catch (UnsupportedEncodingException e) {
                    Logger.sLog("bunti error: " + e.getMessage());
                } catch (ClientProtocolException e) {
                    Logger.sLog("bunti error: " + e.getMessage());
                } catch (IOException e) {
                    Logger.sLog("bunti error: " + e.getMessage());
                }
            }
        };

        new Thread(r).start();
    }

    @Override
    public void setPar56(final int par, final int red, final int green, final int blue) {

        System.out.println("BuntiClient: setPar56 par=" + par + " " + red + "," + green + "," + blue);

        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(baseAddress + "/control/devices");
                    post.addHeader("Content-Type", "application/json");

                    StringBuilder jsonString = new StringBuilder();

                    jsonString.append("{ \"timeStamp\": 0, \"updates\": [ ");

                    jsonString.append(" {\"deviceId\": ");
                    jsonString.append(par);
                    jsonString.append(", \"options\": { \"red\": ");
                    jsonString.append(red);
                    jsonString.append(", \"green\": ");
                    jsonString.append(green);
                    jsonString.append(", \"blue\": ");
                    jsonString.append(blue);
                    jsonString.append(" } } ");

                    jsonString.append("] }");

                    StringEntity entity = new StringEntity( jsonString.toString(), "UTF-8");
                    post.setEntity(entity);

                    client.execute(post);
                    post.abort();

                } catch (UnsupportedEncodingException e) {
                    Logger.sLog("bunti error: " + e.getMessage());
                } catch (ClientProtocolException e) {
                    Logger.sLog("bunti error: " + e.getMessage());
                } catch (IOException e) {
                    Logger.sLog("bunti error: " + e.getMessage());
                }
            }
        };

        new Thread(r).start();
    }

    @Override
    public void setLampel(final boolean red, final boolean yellow, final boolean green) {

        Runnable r = new Runnable() {
            @Override
            public void run() {
                int value = 0;
                if( green ) value |= 0x01;
                if( yellow ) value |= 0x02;
                if( red ) value |= 0x04;

                try {
                    byte buffer[] = ("io set port 2 " + Integer.toHexString(value) + '\n').getBytes();
                    
                    DatagramSocket udp = new DatagramSocket();
                    InetAddress ia = InetAddress.getByName("lampel.raum.ctdo.de");
                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ia, 2701);

                    udp.send(packet);
                } catch (IOException e) {
                    Logger.sLog("lampel error: " + e.getMessage());
                }
            }
        };


        new Thread(r).start();

/*        try {
            HttpPost post = new HttpPost(baseAddress + "/control/devices");
            post.addHeader("Content-Type", "application/json");

            StringEntity entity = new StringEntity(
                    "{ \"timeStamp\": 0, \"updates\": [ {\"deviceId\": 4, \"options\": { \"red\": "+
                            red+", \"green\": "+green+", \"yellow\": "+yellow+" } }  ] }" ,
                    "UTF-8");

            post.setEntity(entity);

            HttpResponse response = client.execute(post);
            System.out.println(response);

            post.abort();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

}
