package com.key.coolkeyboard.keyboard;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.Xml;

import androidx.core.content.ContextCompat;

import com.key.coolkeyboard.CoolKeyboardSkin;
import com.key.coolkeyboard.R;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.tool.StaticValue;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.Objects;

public class CustomViewConfig {
    private String Bg_action_normal = "btn_keyboard_key_functional_normal.9.png";
    private String jpg_BG = "keyboard_background.jpg";
    private String color_normal_key = "key_text_color_normal";
    private String color_action_key = "key_text_color_functional";
    private String Bg_pressed = "btn_keyboard_key_normal_pressed.9.png";
    private String Bg_normal = "btn_keyboard_key_normal_normal.9.png";
    private String path_drawxh = "/drawable-xhdpi-v4/";
    private String path_drawxxh = "/drawable-xxhdpi-v4/";
    private String path_color = "/colors.xml";
    private String icon_del = "sym_keyboard_delete_normal.png";
    private String icon_shift = "sym_keyboard_shift.png";
    private String Bg_action_pressed = "btn_keyboard_key_functional_pressed.9.png";
    private String icon_shift_lock = "sym_keyboard_shift_locked.png";

    private String Bg_space_normal = "btn_keyboard_spacekey_normal_normal.9.png";
    private String Bg_space_pressed = "btn_keyboard_spacekey_normal_pressed.9.png";

    private Drawable BgActionDraw;
    private Drawable BgSpaceDraw;
    private Drawable BgNormalDraw;

    private Drawable iconShift = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.drawable.ico_shift_lit);
    private Drawable iconDel = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.drawable.icon_del);
    private Drawable BG = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.color.black);

    private int keyNoramlcolor = CoolKeyboardSkin.app.getResources().getColor(R.color.white, null);

    private int keyActioncolor = CoolKeyboardSkin.app.getResources().getColor(R.color.white, null);

    private Drawable iconShiftLock = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.drawable.ico_shift_lit);


    public Drawable getBG() {
        return BG;
    }

    public Drawable getBgActionDraw() {
        return BgActionDraw;
    }

    public Drawable getBgNormalDraw() {
        return BgNormalDraw;
    }

    public Drawable getBgSpaceDraw() {
        return BgSpaceDraw;
    }

    public Drawable getIconDel() {
        return iconDel;
    }

    public Drawable getIconShift() {
        return iconShift;
    }

    public Drawable getIconShiftLock() {
        return iconShiftLock;
    }

    public int getKeyNoramlcolor() {
        return keyNoramlcolor;
    }


    public int getKeyActioncolor() {
        return keyActioncolor;
    }

    public void init() {
//        iconShift = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.drawable.ico_shift_lit);
//        iconDel = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.drawable.icon_del);
//        BG = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.color.white);
//        keyNoramlcolor = CoolKeyboardSkin.app.getResources().getColor(R.color.white, null);
//        iconShiftLock = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.drawable.ico_shift_lit);
//        Drawable drawable1 = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.drawable.de_keybg_press);
//        Drawable drawable = ContextCompat.getDrawable(CoolKeyboardSkin.app, R.drawable.de_keybg);
//        StateListDrawable status = Mytool.getStatus(drawable, drawable1);
//        BgActionDraw = status;
//        BgNormalDraw = status;
//        BgSpaceDraw = status;
    }

    private Drawable getKeyBackGround(Context context, String resDirPath, String drawName) {
        String filePath = resDirPath + path_drawxh + drawName;
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), BitmapFactory.decodeFile(filePath));
        return bitmapDrawable;
    }

    private Drawable getBackGround(Context context, String resDirPath) {
        String filePath = resDirPath + path_drawxxh + jpg_BG;
        if (!new File(filePath).exists()) {
            return null;
        }
        BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), BitmapFactory.decodeFile(filePath));
        return bitmapDrawable;
    }

    private void updateKeyColor(String resDirPath) {

        String colorXmlPath = resDirPath + path_color;
        File file = new File(colorXmlPath);
        if (!file.exists()) {
            return;
        }
        try {
            XmlPullParser xmlPullParser = Xml.newPullParser();

            String s = Mytool.fileToString(file);
            xmlPullParser.setInput(new StringReader(s));
            int eventT = xmlPullParser.getEventType();
            while (eventT != XmlPullParser.END_DOCUMENT) {
                if (eventT == XmlPullParser.START_TAG && (Objects.equals(xmlPullParser.getName(), "color") || Objects.equals(xmlPullParser.getName(), "item"))) {
                    String value = xmlPullParser.getAttributeValue(null, "name");
                    if (value.equals(color_normal_key)) {
                        keyNoramlcolor = Color.parseColor(xmlPullParser.nextText());

                    }
                    if (value.equals(color_action_key)) {
                        keyActioncolor = Color.parseColor(xmlPullParser.nextText());

                    }
                }
                eventT = xmlPullParser.next();
            }
        } catch (XmlPullParserException exception) {

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public void updateConfig(Context con) {
        String resDirPath = StaticValue.PATH;
        if (!resDirPath.isEmpty()) {
            updateKeyColor(resDirPath);
            BG = getBackGround(con, resDirPath);
            BgNormalDraw = getKeyBackGround(con, resDirPath, Bg_normal);
            BgActionDraw = getKeyBackGround(con, resDirPath, Bg_action_normal);
            BgSpaceDraw = getKeyBackGround(con, resDirPath, Bg_space_normal);
            iconDel = getKeyBackGround(con, resDirPath, icon_del);
            iconShift = getKeyBackGround(con, resDirPath, icon_shift);
            iconShiftLock = getKeyBackGround(con, resDirPath, icon_shift_lock);
        }
    }


}
