import java.awt.*;
import context.Store;
import java.net.*;
import org.w3c.dom.*;
import net.sourceforge.wurfl.wurflapi.*;

public class dcn {

    // functions not in the spec, for debugging purposes
    public static String userAgent() {
        return Store.userAgent;
    }

    public static String wurflId() {
        String id= Store.uam.getDeviceIDFromUALoose(Store.userAgent);
        return id;
    }

    public static String wurflDevice() {
        String id= Store.uam.getDeviceIDFromUALoose(Store.userAgent);
        return Store.cm.getCapabilityForDevice(id,"brand_name") +" "+ Store.cm.getCapabilityForDevice(id,"model_name") ;
    }

    public static String serverInfo() {
        try {
         return InetAddress.getLocalHost().toString();
        } catch (UnknownHostException e) {
            return "UnknownHostException";
        }
    }

    // CSS media queries functions

    public static double cssmqWidth(String units) {
        return cssmqWidth(units,0);
    }

    public static double cssmqWidth(String units, double defaultValue) {
        try {
            String id= Store.uam.getDeviceIDFromUALoose(Store.userAgent);
            int widthInPixels=Integer.decode(Store.cm.getCapabilityForDevice(id,"resolution_width"));
            if (units.equals("px"))
                return widthInPixels;
            else {
                // WURFL doesn't indicate physical size of the screen
                 if (units.equals("in"))
                     return defaultValue;
                 else if (units.equals("cm"))
                     return defaultValue;
                 else if (units.equals("mm"))
                     return defaultValue;
                 else if (units.equals("pt"))
                     return defaultValue;
                 else if (units.equals("pc"))
                     return defaultValue;
                return Double.NaN; // WURFL doesn't indicate physical size of the screen
            }
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static double  cssmqHeight(String units) {
        return cssmqHeight(units,0);
    }

    public static double cssmqHeight(String units, double defaultValue) {
        try {
            String id= Store.uam.getDeviceIDFromUALoose(Store.userAgent);
            int heightInPixels=Integer.decode(Store.cm.getCapabilityForDevice(id,"resolution_height"));
            if (units.equals("px"))
                return heightInPixels;
            else {
                 if (units.equals("in"))
                     return defaultValue;
                 else if (units.equals("cm"))
                     return defaultValue;
                 else if (units.equals("mm"))
                     return defaultValue;
                 else if (units.equals("pt"))
                     return defaultValue;
                 else if (units.equals("pc"))
                     return defaultValue;
                return Double.NaN;
            }
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static double cssmqDeviceWidth(String units, double defaultValue) {
        return defaultValue;
    }

    public static double cssmqDeviceWidth(String units) {
        return cssmqDeviceWidth(units,0);
    }

    public static double cssmqDeviceHeight(String units, double defaultValue) {
        return defaultValue;
    }

    public static double cssmqDeviceHeight(String units) {
        return cssmqDeviceHeight(units,0);
    }

    public static String cssmqDeviceAspectRatio(String defaultValue) {
        return defaultValue;
    }
        
    public static String cssmqDeviceAspectRatio() {
        return "";
    }

    public static double cssmqDeviceAspectRatioWidth() {
        return 0;
    }

    public static double cssmqDeviceAspectRatioWidth(double defaultValue) {
        return defaultValue;
    }

    public static double cssmqDeviceAspectRatioHeight() {
        return 0;
    }

    public static double cssmqDeviceAspectRatioHeight(double defaultValue) {
        return defaultValue;
    }

    public static double cssmqColor() {
        //This function returns the number of bits per color supported
        //by the display associated with the device. The result is
        //expressed as a positive integer.
        return cssmqColor(0);
    }

    public static double cssmqColor(double defaultValue) {
        //This function returns the number of bits per color supported
        //by the display associated with the device. The result is
        //expressed as a positive integer.
        String id= Store.uam.getDeviceIDFromUALoose(Store.userAgent);
        try {
            int colors=Integer.decode(Store.cm.getCapabilityForDevice(id,"colors"));
            int log2=0;
            while (colors>1) { colors/=2;log2++; }
            return log2;
        } catch (Exception e) {
            return defaultValue;
        }
    }

    //----------------------------------------------------------------------------
    // This function returns the number of entries in the color look
    // up table of the display associated with the device. The result
    // is expressed as a positive integer.

    // not in WURFL
    public static double cssmqColorIndex() {
        return 0;
    }

    public static double cssmqColorIndex(double defaultValue) {
        return defaultValue;
    }

    //----------------------------------------------------------------------------
    // This function returns the number of bits used to represent
    // monochrome output by the display associated with the
    // device. The result is expressed as a positive integer.
    public static double cssmqMonochrome() {
        return cssmqMonochrome(0);
    }

    public static double cssmqMonochrome(double defaultValue) {
        String id= Store.uam.getDeviceIDFromUALoose(Store.userAgent);
        String greyscale=Store.cm.getCapabilityForDevice(id,"wallpaper_greyscale");
        if (greyscale=="true")
            return Integer.decode(Store.cm.getCapabilityForDevice(id,"wallpaper_colors"));
        else
            return 0;
    }

    //----------------------------------------------------------------------------
    // This function returns the resolution of the display associated
    // with the device as a decimal number.
    public static double cssmqResolution(String units) {
        return 0;
    }
    public static double cssmqResolution(String units, double defaultValue) {
        return 0;
    }

    //----------------------------------------------------------------------------
    // This function returns the resolution of the display associated
    // with the device as a decimal number.
    public static String cssmqScan() {
        return "";
    }

    public static String cssmqScan(String defaultValue) {
        return "";
    }

    //----------------------------------------------------------------------------
    // This function returns a boolean value of true if the device is
    // grid based. Grid based devices support a regular character
    // array rather than a bitmap display. Examples of grid based
    // devices include teletype terminals and simple mobile phones
    // with fixed width fonts.
    public static Boolean cssmqGrid() {
        return false;
    }

    public static Boolean cssmqGrid(Boolean defaultValue) {
        return false;
    }
}
