package com.input.board.appskey.keyboard.customer;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.SurfaceView;

import androidx.core.graphics.drawable.DrawableCompat;

import com.input.board.appskey.keyboard.R;



public class CustomInputView extends MyKeyBoardView {

    private Paint mPaint;
    private Context mContext;

    private float mRation = 0.5f;


    //0 小写 1 大写 2 大写锁定
    private int isLowerCase = 0;
    //0 默认键盘 1 字母键盘 2 符号键盘
    private int mMode = 0;

    private KeyBoardSkin keyBoardSkin;

    public CustomInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setAttribute(attrs, context);
    }

    public CustomInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        setAttribute(attrs, context);
    }

    public CustomInputView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        setAttribute(attrs, context);
    }


    public void setMode(int mType) {
        this.mMode = mType;
    }

    public int getMode() {
        return mMode;
    }

    public int isLowerCase() {
        return isLowerCase;
    }

    public void setLowerCase(int lowerCase) {
        isLowerCase = lowerCase;
    }

    public void updateUi(SurfaceView surfaceView) {
        keyBoardSkin.updateSkinConfig(surfaceView);
        invalidate();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextAlign(Paint.Align.CENTER);

    }

    private void setAttribute(AttributeSet attrs, Context con) {
        keyBoardSkin = new KeyBoardSkin(con);
        initPaint();
        TypedArray mTypedArray = con.obtainStyledAttributes(attrs, R.styleable.CustomInputView);

        int color = mTypedArray.getColor(R.styleable.CustomInputView_text_color_done, 1);

        Drawable drawable = mTypedArray.getDrawable(R.styleable.CustomInputView_drawable_cancel);

        int textSize = mTypedArray.getInt(R.styleable.CustomInputView_text_size_key, 12);

        mTypedArray.recycle();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (MyKeyBoard.Key curKey : getKeyboard().getKeys()) {
            int code = curKey.codes[0];
            realDraw(curKey, canvas, code);

        }

    }
    private void realDraw(MyKeyBoard.Key curKey, Canvas canvas, int code) {
        switch (code) {
            case KeyCodeConst.KEY_CODE_SHIFT:
                drawAllShift(curKey, canvas);
                break;
            case KeyCodeConst.KEY_CODE_NUMBER_SHIFT:
                onDrawCurKey(curKey, canvas, "More", keyBoardSkin.getFunctionDraw(), null);
                break;
            case KeyCodeConst.KEY_CODE_DELETE:
                onDrawCurKey(curKey, canvas, "Delete", keyBoardSkin.getFunctionDraw(),null);
                break;
            case KeyCodeConst.KEY_CODE_SYMBOL_SHIFT:
                onDrawCurKey(curKey, canvas, "123", keyBoardSkin.getFunctionDraw(), null);
                break;
            case KeyCodeConst.KEY_CODE_CHANGE_NUMBER:
                onDrawCurKey(curKey, canvas, null, keyBoardSkin.getToDraw(), null);
                break;
            case KeyCodeConst.KEY_CODE_BACK:
                onDrawCurKey(curKey, canvas, "Back", keyBoardSkin.getToDraw(), null);
                break;
            case KeyCodeConst.KEY_CODE_SPACE:
                onDrawCurKey(curKey, canvas, null, keyBoardSkin.getSpaceDraw(), null);
                break;
            case KeyCodeConst.KEY_CODE_COMPLETE:
                onDrawCurKey(curKey, canvas, "Done", keyBoardSkin.getFunctionDraw(), null);
                break;
            case KeyCodeConst.KEY_CODE_CANCEL:
                onDrawCurKey(curKey, canvas, "Done", keyBoardSkin.getFunctionDraw(), null);
                break;
            default:

                onDrawCurKey(curKey, canvas, null, keyBoardSkin.getGeneralDraw(), null);
                break;


        }
    }

    private void drawAllShift(MyKeyBoard.Key curKey, Canvas canvas) {
        if (isLowerCase == 0) {
            onDrawCurKey(curKey, canvas, "Shift", keyBoardSkin.getFunctionDraw(), null);
        } else if (isLowerCase == 1) {
            onDrawCurKey(curKey, canvas, "Shift", keyBoardSkin.getFunctionDraw(),null);
        } else if(isLowerCase == 2){
            onDrawCurKey(curKey, canvas, "Shift", keyBoardSkin.getFunctionDraw(), null);
        }
    }

    private void onDrawCurKey(MyKeyBoard.Key curKey, Canvas curCanvas, String label, Drawable bgDrawable, Drawable iconDraw) {

        if (bgDrawable != null) {
            onDrawKeyBackground(curKey, curCanvas, bgDrawable);
        }
        if (iconDraw != null) {
            onDrawKeyIcon(curKey, curCanvas, iconDraw);
        }
        onDrawKeyText(curKey, curCanvas, label);
    }

    private void onDrawKeyText(MyKeyBoard.Key curKey, Canvas curCanvas, String label) {
        mPaint.setColor(keyBoardSkin.getKeyTextColor());
        mPaint.setTextSize(mContext.getResources().getDimension(R.dimen.text_size));
        float v = curKey.width / 2f;
        float v1 = curKey.height / 2f;
        float v2 = (mPaint.getTextSize() - mPaint.descent()) / 2f;


        if (curKey.label != null) {
            curCanvas.drawText((String) curKey.label, curKey.x + getPaddingLeft() + v, curKey.y + getPaddingRight() + v1 + v2, mPaint);
        } else if (label != null) {
            curCanvas.drawText(label, curKey.x + getPaddingLeft() + v, curKey.y + getPaddingRight() + v1 + v2, mPaint);
        }


    }

    private void onDrawKeyBackground(MyKeyBoard.Key curKey, Canvas curCanvas, Drawable curDrawable) {
        if (curKey.codes[0] != 0) {
            curDrawable.setState(curKey.getCurrentDrawableState());
        }
        Rect rect = new Rect((curKey.x + this.getPaddingLeft()), (curKey.y + this.getPaddingTop()), (curKey.x + this.getPaddingLeft() + curKey.width), (curKey.y + this.getPaddingTop() + curKey.height));
        curDrawable.setBounds(rect);

        curDrawable.draw(curCanvas);


    }

    private void onDrawKeyIcon(MyKeyBoard.Key curKey, Canvas curCanvas, Drawable curDrawable) {

        Drawable wrap = DrawableCompat.wrap(curDrawable);

        curKey.icon = curDrawable;
        float iconW = (float) curKey.icon.getIntrinsicWidth();
        float iconH = (float) curKey.icon.getIntrinsicHeight();

        float wDivRation = iconW / curKey.width;
        float hDivRation = iconH / curKey.height;

        curKey.icon.draw(curCanvas);

        if (wDivRation > hDivRation) {
            float minRatio = 0;
            if (wDivRation <= mRation) {
                minRatio = wDivRation;
            } else {
                minRatio = mRation;
            }
            iconH = (iconH / wDivRation) * minRatio;
            iconW = (iconW / wDivRation) * minRatio;
        } else {
            float minRatio = 0;
            if (hDivRation <= mRation) {
                minRatio = hDivRation;
            } else {
                minRatio = mRation;
            }
            iconH = (iconH / hDivRation) * minRatio;
            iconW = (iconW / hDivRation) * minRatio;
        }
        float subW = (curKey.width - iconW) / 2f;
        float subH = (curKey.height - iconH) / 2f;
        int xLeft = (int) (curKey.x + getPaddingLeft() + subW);
        int yTop = (int) (curKey.y + getPaddingTop() + subH);
        int xRight = (int) (xLeft + iconW);
        int yBottom = (int) (yTop + iconH);
        curKey.icon.setBounds(xLeft, yTop, xRight, yBottom);
        curKey.icon.draw(curCanvas);


    }

}
