package com.mocircle.cidrawing.core;

import android.graphics.Paint;

import com.mocircle.cidrawing.persistence.Persistable;
import com.mocircle.cidrawing.persistence.PersistenceException;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class CiPaint extends Paint implements Persistable {

    private static final String KEY_COLOR = "color";
    private static final String KEY_STROKE_WIDTH = "strokeWidth";
    private static final String KEY_FLAGS = "flags";
    private static final String KEY_ALPHA = "alpha";
    private static final String KEY_STYLE = "style";
    private static final String KEY_SECONDARY_COLOR = "secondaryColor";

    private Integer secondaryColor;

    public CiPaint() {
        super();
    }

    public CiPaint(int flags) {
        super(flags);
    }

    public CiPaint(Paint paint) {
        super(paint);
        if (paint instanceof CiPaint) {
            CiPaint p = (CiPaint) paint;
            secondaryColor = p.secondaryColor;
        }
    }

    public Integer getSecondaryColor() {
        return secondaryColor;
    }

    public void setSecondaryColor(Integer color) {
        secondaryColor = color;
    }

    @Override
    public JSONObject generateJson() {
        JSONObject object = new JSONObject();
        try {
            object.put(KEY_COLOR, this.getColor());
            object.put(KEY_STROKE_WIDTH, this.getStrokeWidth());
            object.put(KEY_FLAGS, this.getFlags());
            object.put(KEY_ALPHA, this.getAlpha());
            object.put(KEY_STYLE, this.getStyle().toString());
            object.put(KEY_SECONDARY_COLOR, this.getSecondaryColor());
        } catch (JSONException e) {
            throw new PersistenceException(e);
        }
        return object;
    }

    @Override
    public Map<String, byte[]> generateResources() {
        return null;
    }

    @Override
    public void loadFromJson(JSONObject object, Map<String, byte[]> resources) {
        if (object != null) {
            try {
                setColor(object.getInt(KEY_COLOR));
                setStrokeWidth((float) object.getDouble(KEY_STROKE_WIDTH));
                setFlags(object.getInt(KEY_FLAGS));
                setAlpha(object.getInt(KEY_ALPHA));
                setStyle(Style.valueOf(object.getString(KEY_STYLE)));
                if (object.has(KEY_SECONDARY_COLOR)) {
                    secondaryColor = object.getInt(KEY_SECONDARY_COLOR);
                }
            } catch (JSONException e) {
                throw new PersistenceException(e);
            }
        }
    }

    @Override
    public void afterLoaded() {
    }
}

@@ -62,16 +62,177 @@
 .Paint;%0A
+import android.graphics.PorterDuff;%0Aimport android.graphics.PorterDuffXfermode;%0Aimport android.graphics.Xfermode;%0A%0Aimport com.mocircle.android.logging.CircleLog;
 %0Aimport 
@@ -404,16 +404,48 @@
 bject;%0A%0A
+import java.lang.reflect.Field;%0A
 import j
@@ -520,16 +520,66 @@
 able %7B%0A%0A
+    private static final String TAG = %22CiPaint%22;%0A%0A
     priv
@@ -919,16 +919,75 @@
 yColor%22;
+%0A    private static final String KEY_XFERMODE = %22xfermode%22;
 %0A%0A    pr
@@ -1955,32 +1955,108 @@
 ondaryColor());%0A
+            object.put(KEY_XFERMODE, getXfermodeAsInt(this.getXfermode()));%0A
         %7D catch 
@@ -2840,32 +2840,180 @@
               %7D%0A
+                if (object.has(KEY_XFERMODE)) %7B%0A                    setXfermode(getXfermodeFromInt(object.getInt(KEY_XFERMODE)));%0A                %7D%0A
             %7D ca
@@ -3170,10 +3170,743 @@
 %7B%0A    %7D%0A
+%0A%0A    private int getXfermodeAsInt(Xfermode mode) %7B%0A        if (mode == null) %7B%0A            return -1;%0A        %7D%0A        try %7B%0A            Field field = Xfermode.class.getDeclaredField(%22porterDuffMode%22);%0A            field.setAccessible(true);%0A            return field.getInt(mode);%0A        %7D catch (NoSuchFieldException e) %7B%0A            CircleLog.w(TAG, e);%0A        %7D catch (IllegalAccessException e) %7B%0A            CircleLog.w(TAG, e);%0A        %7D%0A        return -1;%0A    %7D%0A%0A    private Xfermode getXfermodeFromInt(int mode) %7B%0A        if (mode %3C 0) %7B%0A            return null;%0A        %7D else %7B%0A            PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.values()%5Bmode%5D);%0A            return xfermode;%0A        %7D%0A    %7D%0A
 %7D%0A

Save xfermode for painter object