package org.ow2.chameleon.fuchsia.philips.hue.importer;

import com.philips.lighting.hue.sdk.utilities.PHUtilities;
import com.philips.lighting.model.PHBridge;
import com.philips.lighting.model.PHLight;
import com.philips.lighting.model.PHLightState;
import org.apache.felix.ipojo.annotations.*;
import org.apache.felix.service.command.Descriptor;
import org.osgi.framework.BundleContext;

import java.util.Random;
import java.util.Set;

@Component(immediate = true)
@Instantiate
@Provides
public class PhilipsHueGogoCommand {

    @ServiceProperty(name = "osgi.command.scope", value = "philips")
    String m_scope;

    @ServiceProperty(name = "osgi.command.function", value = "{}")
    String[] m_function = new String[]{"phlist","phset"};

    @Requires(specification = "com.philips.lighting.model.PHLight")
    Set<PHLight> lights;

    private static final int MAX_HUE=65535;

    @Requires
    PHBridge bridge;

    private BundleContext m_context = null;

    public PhilipsHueGogoCommand(BundleContext context) {
        this.m_context = context;
    }

    @Descriptor  (value = "phlist")
    public void phlist(@Descriptor("phlist") String... parameters) {
        for(PHLight light:lights){
            System.out.println(light.toString());
            System.out.println("state:"+light.getLastKnownLightState().isOn());
        }
    }

    @Descriptor  (value = "phset")
    public void phset(@Descriptor("phset") String... parameters) {

        String valueStr=getArgumentValue("-value",parameters);
        String nameStr=getArgumentValue("-name",parameters);
        String ttStr=getArgumentValue("-tt",parameters);
        String iStr=getArgumentValue("-i",parameters);
        String rStr=getArgumentValue("-r",parameters);
        String gStr=getArgumentValue("-g",parameters);
        String bStr=getArgumentValue("-b",parameters);

        Boolean value=new Boolean(valueStr);

        for(PHLight light:lights){

            if(nameStr==null || nameStr.equals(light.getName())){

                PHLightState lightState = new PHLightState();

                if(value!=null){
                    lightState.setOn(value);
                }

                if(ttStr!=null){
                    System.out.println("transition time:"+ttStr);
                    lightState.setTransitionTime(Integer.valueOf(ttStr));
                }

                if(iStr!=null){
                    System.out.println("brightness:"+iStr);
                    lightState.setBrightness(Integer.valueOf(iStr));
                }

                if(rStr!=null||gStr!=null||bStr!=null){

                    int r=rStr!=null?Integer.valueOf(rStr):0;
                    int g=gStr!=null?Integer.valueOf(gStr):0;
                    int b=bStr!=null?Integer.valueOf(bStr):0;

                    System.out.println(String.format("color %s %s %s",r,g,b));

                    System.out.println("model:"+light.getModelNumber());

                    float[] xy= PHUtilities.calculateXYFromRGB(r, g, b, light.getIdentifier());

                    lightState.setHue(PHUtilities.colorFromXY(xy,light.getIdentifier()));
                    lightState.setColorMode(PHLight.PHLightColorMode.COLORMODE_HUE_SATURATION);

                    Random rand = new Random();
                    lightState.setHue(rand.nextInt(MAX_HUE));

                }


                bridge.updateLightState(light,lightState);

            }
        }
    }

    private String getArgumentValue(String option, String... params) {

        boolean found = false;
        String value = null;

        for (int i = 0; i < params.length; i++) {
            if (i < (params.length - 1) && params[i].equals(option)) {
                found = true;
                value = params[i + 1];
                break;
            }
        }

        if (found)
            return value;

        return null;
    }

}

@@ -835,53 +835,8 @@
 s;%0A%0A
-    private static final int MAX_HUE=65535;%0A%0A
     
@@ -1038,22 +1038,41 @@
 alue = %22
-phlist
+List all lights available
 %22)%0A    p
@@ -1348,56 +1348,275 @@
  = %22
-phset%22)%0A    public void phset(@Descriptor(%22phset
+Change light parameters, for a specific lamp or for all light plugged into the bridge%22)%0A    public void phset(@Descriptor(%22%5B-name NAME%5D %5B-on true%7Cfalse%5D %5B-i 0%3C=INTENSITY%3C=255%5D %5B-tt TRANSITION_TIME_IN_CENTISECONDS%5D %5B-r 0%3C=RED%3C=255%5D %5B-g 0%3C=GREEN%3C=255%5D %5B-b 0%3C=BLUE%3C=255%5D
 %22) S
@@ -1682,21 +1682,18 @@
 Value(%22-
-value
+on
 %22,parame
@@ -3027,14 +3027,29 @@
 lor 
-%25s %25s 
+red:%25s green:%25s blue:
 %25s%22,
@@ -3082,58 +3082,81 @@
     
-System.out.println(%22model:%22+light.getModelNumber()
+lightState.setColorMode(PHLight.PHLightColorMode.COLORMODE_HUE_SATURATION
 );%0A%0A
@@ -3338,24 +3338,25 @@
 tifier()));%0A
+%0A
             
@@ -3363,85 +3363,68 @@
     
-    lightState.setColorMode(PHLight.PHLightColorMode.COLORMODE_HUE_SATURATION
+%7D%0A%0A%0A                bridge.updateLightState(light,lightState
 );%0A%0A
@@ -3435,24 +3435,26 @@
         
+%7D%0A
         
 Random r
@@ -3449,209 +3449,300 @@
     
-Random rand = new Random();
+%7D%0A    %7D%0A
 %0A    
+/**%0A
      
-           lightState.setHue(rand.nextInt(MAX_HUE));%0A%0A                %7D%0A%0A%0A                bridge.updateLightState(light,lightState);%0A%0A            %7D%0A        %7D%0A    %7D%0A
+* Look up for the value of a parameter in a sequence of string%0A     * @param option the option that we are looking for. e.g. '-k'%0A     * @param params the list of parameters%0A     * @return the value associated with the parameter or Null in case it is not present%0A     */
 %0A   

add help for philips hue command line