package org.twopm.laf;

import java.io.Serializable;
import javax.swing.plaf.ColorUIResource;
import static org.twopm.laf.Color.ColorClass.AT;
import static org.twopm.laf.Color.ColorClass.SF;

/**
 * @author Edward M. Kagan <pagan@tabor.one>
 */
@SuppressWarnings("rawtypes")
public class Color implements Serializable{

    public void printJava() {
       
        String res = "color_map.add(";
        res += this.getString();
        res += ");";
        System.out.println(res);
    }
    
        public String getString() {
       
        String res = "new org.twopm.laf.Color(\"";
        res += this.pname + "\", ";
        
        if (this.cls ==SF)
        {
           res += "org.twopm.laf.Color.ColorClass.SF, ";
        }
        else
        {
           res += "org.twopm.laf.Color.ColorClass.AT, ";
        }
        
        res += R + ", ";
        res += G + ", ";
        res += B + ", ";
        res += A + ", ";
        res += "\"" +this.description + "\"";
        
        res += ")";
        return res;
    }
    

    
    public enum ColorClass {
        SF (javax.swing.plaf.ColorUIResource.class),
        AT (java.awt.Color.class);

            private final Class cls;

            ColorClass(Class cls) {
                this.cls = cls;
            }
            
            public Class getClazz()
            {
                return this.cls;
            }
    }
    
    int R;
    int G;
    int B;
    int A;
    String pname;
    ColorClass cls;
    String description;
    
    
    public Color(javax.swing.plaf.ColorUIResource color) {
        this.A = color.getAlpha();
        this.B = color.getBlue();
        this.G = color.getGreen();
        this.R = color.getRed();   
        this.pname = "_";
        this.cls = SF;
        this.description = "";
    }
    
        public Color(String pname, ColorClass cls, int red, int green, int blue, int alpha, String desc) {
        this.A = alpha;
        this.B = blue;
        this.G = green;
        this.R = red;   
        this.pname = pname;
        this.cls = cls;
        this.description = desc;
    }

    @Override
    public String toString() {
        return this.pname;
    }
    
    public String toStringC() {
        return "C[" + this.pname + ":" + this.cls + ":" + R + ","  + G + "," + B + "," + A + ":" + description + "]";
    }
    
    public Color (String str)
    {
        if (str.startsWith("C"))
        {
            String real = str.substring(2, str.length() - 1);
            String [] vals = real.split(":");
            
            this.pname = vals[0];
            if (vals[1].equals("SF"))
            {
                this.cls = SF;
            }
            else if (vals[1].equals("AT"))
            {
                this.cls = AT;
            }
            
            this.description = vals[3];
            
            vals = vals[2].split(",");
            
            
            this.R = Integer.parseInt(vals[0]);
            this.G = Integer.parseInt(vals[1]);
            this.B = Integer.parseInt(vals[2]);
            this.A = Integer.parseInt(vals[3]);
            
        }
        else
        {
            System.err.println("FUCK OFF - poor format");
        }
        
        
    }
    
    public Object getObject ()
    {
        java.awt.Color cc = new java.awt.Color(R, G, B, A);
        switch (this.cls)
        {
        
            case SF : {
                javax.swing.plaf.ColorUIResource res = new ColorUIResource(cc);
                return res;
            }
            case AT : {
                return cc;
            }
        }
        return null;
    }
    
    public Object getName() {
        return this.pname;
    }

    public String getDescription() {
        return description;
    }

    public void setA(int A) {
        this.A = A;
    }

    public void setB(int B) {
        this.B = B;
    }

    public void setG(int G) {
        this.G = G;
    }

    public void setR(int R) {
        this.R = R;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Color() {
    }

    public String getPname() {
        return pname;
    }
    
    public void setObject ( java.awt.Color obj)
    {
        this.A = obj.getAlpha();
        this.B = obj.getBlue();
        this.G = obj.getGreen();
        this.R = obj.getRed();   
    }
    
    public String getHEX ()
    {
        java.awt.Color cc = new java.awt.Color(R, G, B, A);
        return String.format("#%06X", (0xFFFFFF & cc.getRGB()));
    }
     
}

