package com.gmail.alexellingsen.g2skintweaks.hooks;

import android.content.res.XModuleResources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.gmail.alexellingsen.g2skintweaks.Prefs;
import com.gmail.alexellingsen.g2skintweaks.R;
import com.gmail.alexellingsen.g2skintweaks.utils.SettingsHelper;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_InitPackageResources.InitPackageResourcesParam;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class LGLockScreenHook {

    private static final String PACKAGE = "com.android.keyguard";
    private static final String USE_PACKAGE = "com.lge.lockscreen";

    private static SettingsHelper mSettings;

    private static Bitmap mBitmap1;
    private static Bitmap mBitmap2;
    private static Bitmap mBitmap3;

    private static Bitmap mOriginalBitmap1;
    private static Bitmap mOriginalBitmap2;
    private static Bitmap mOriginalBitmap3;

    public static void init(SettingsHelper settings) {
        mSettings = settings;
    }

    public static void handleInitPackageResources(InitPackageResourcesParam resparam, XModuleResources modRes) {
        if (!resparam.packageName.equals(PACKAGE)) {
            return;
        }

        Drawable d1 = modRes.getDrawable(R.drawable.indicator_code_lock_point_area_default_holo);
        Drawable d2 = modRes.getDrawable(R.drawable.indicator_code_lock_point_area_green_holo);
        Drawable d3 = modRes.getDrawable(R.drawable.indicator_code_lock_point_area_red_holo);

        mBitmap1 = drawableToBitmap(d1);
        mBitmap2 = drawableToBitmap(d2);
        mBitmap3 = drawableToBitmap(d3);
    }

    private static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

    public static void handleLoadPackage(final LoadPackageParam lpparam) {
        if (!lpparam.packageName.equals(PACKAGE)) {
            return;
        }

        Class<?> findClass;

        try {
            findClass = XposedHelpers.findClass(
                    USE_PACKAGE + ".widget.pattern.LgeLockPatternView",
                    lpparam.classLoader
            );
        } catch (Throwable e) {
            XposedBridge.log(e);
            XposedBridge.log("Couldn't find class");
            return;
        }

        XposedHelpers.findAndHookMethod(
                findClass,
                "drawCircle",
                "android.graphics.Canvas",
                "int",
                "int",
                "boolean",

                new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        if (mOriginalBitmap1 == null || mOriginalBitmap2 == null || mOriginalBitmap3 == null) {
                            mOriginalBitmap1 = (Bitmap) XposedHelpers.getObjectField(param.thisObject, "mBitmapCircleDefault");
                            mOriginalBitmap2 = (Bitmap) XposedHelpers.getObjectField(param.thisObject, "mBitmapCircleGreen");
                            mOriginalBitmap3 = (Bitmap) XposedHelpers.getObjectField(param.thisObject, "mBitmapCircleRed");
                        }

                        if (mSettings.getBoolean(Prefs.ENABLE_AOSP_PATTERN_DOTS, false)) {
                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleDefault", mBitmap1);
                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleGreen", mBitmap2);
                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleRed", mBitmap3);
                        } else {
                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleDefault", mOriginalBitmap1);
                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleGreen", mOriginalBitmap2);
                            XposedHelpers.setObjectField(param.thisObject, "mBitmapCircleRed", mOriginalBitmap3);
                        }
                    }
                }
        );
    }
}
@@ -1056,32 +1056,29 @@
 tic Bitmap m
-Original
+Stock
 Bitmap1;%0A   
@@ -1097,32 +1097,29 @@
 tic Bitmap m
-Original
+Stock
 Bitmap2;%0A   
@@ -1142,24 +1142,21 @@
 Bitmap m
-Original
+Stock
 Bitmap3;
@@ -3293,21 +3293,83 @@
     
-if (mOriginal
+// Store stock bitmaps to be used later.%0A                        if (mStock
 Bitm
@@ -3380,32 +3380,29 @@
 == null %7C%7C m
-Original
+Stock
 Bitmap2 == n
@@ -3409,24 +3409,21 @@
 ull %7C%7C m
-Original
+Stock
 Bitmap3 
@@ -3458,32 +3458,29 @@
            m
-Original
+Stock
 Bitmap1 = (B
@@ -3583,32 +3583,29 @@
            m
-Original
+Stock
 Bitmap2 = (B
@@ -3710,24 +3710,21 @@
        m
-Original
+Stock
 Bitmap3 
@@ -3916,24 +3916,86 @@
 , false)) %7B%0A
+                            // Set bitmaps to custom bitmaps.%0A
             
@@ -4335,24 +4335,111 @@
    %7D else %7B%0A
+                            // Set bitmaps back to normal, not sure if this is needed.%0A
             
@@ -4522,32 +4522,29 @@
 eDefault%22, m
-Original
+Stock
 Bitmap1);%0A  
@@ -4635,32 +4635,29 @@
 cleGreen%22, m
-Original
+Stock
 Bitmap2);%0A  
@@ -4754,16 +4754,13 @@
 %22, m
-Original
+Stock
 Bitm

Rename variables & add comments