package com.key.simplekeyboard.keyboard;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;

import com.key.simplekeyboard.tools.CustomViewConfig;
import com.key.simplekeyboard.tools.Mytools;
import com.key.simplekeyboard.tools.drawIcon;

import java.util.List;

public class MyKeyboard extends KeyboardView {
    private Paint mPaint;
    private int viewType = 0;
    private int shiftType = 0;
    private CustomViewConfig config;

    public MyKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public static class KeyBoard extends Keyboard {

        public static final int KEYCODE_SHIFT_MORE = -360;
        public static final int KEYCODE_SHIFT_123 = -361;

        public KeyBoard(Context context, int layoutTemplateResId) {
            super(context, layoutTemplateResId);
        }
    }

    public int getShiftType() {
        return shiftType;
    }

    public void setShiftType(int shiftType) {
        this.shiftType = shiftType;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public int getViewType() {
        return viewType;
    }

    @Override
    public void onDraw(Canvas canvas) {
        KeyBoard keyboard = (KeyBoard) getKeyboard();
        List<Keyboard.Key> keys = keyboard.getKeys();
        for (int r = 0; r < keys.size(); r++) {
            Keyboard.Key key = keys.get(r);
            int code = key.codes[0];
            mPaint.setColor(config.getKeyActioncolor());
            if (code == KeyBoard.KEYCODE_MODE_CHANGE) {

                onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
                onDrawLabel(key, canvas);

            } else if (code == KeyBoard.KEYCODE_SHIFT) {
                onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
                drawIcon.onDrawKeyIcon(key, getShiftDraw(), canvas, this);
            } else if (code == KeyBoard.KEYCODE_SHIFT_123) {
                onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
                onDrawLabel(key, canvas);

            } else if (code == KeyBoard.KEYCODE_SHIFT_MORE) {
                onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
                onDrawLabel(key, canvas);

            } else if (code == KeyBoard.KEYCODE_DONE) {
                onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
                onDrawLabel(key, canvas);
            } else if (code == KeyBoard.KEYCODE_DELETE) {
                onDrawKeyBackground(key, config.getBgActionDraw(), canvas);
                drawIcon.onDrawKeyIcon(key, config.getIconDel(), canvas, this);
                onDrawLabel(key, canvas);
            } else {
                mPaint.setColor(config.getKeyNoramlcolor());
                onDrawKeyBackground(key, config.getBgNormalDraw(), canvas);
                onDrawLabel(key, canvas);
            }
        }

    }

    private Drawable getShiftDraw() {
        if (shiftType == 0) {
            return config.getIconShift();
        } else {
            return config.getIconShiftLock();
        }
    }

    private void initView() {
        config = new CustomViewConfig();
        mPaint = new Paint();
        config.init();
        float texsize = Mytools.spToPpx(20f, this.getContext());
        mPaint.setTextSize(texsize);
    }

    public void updateConfigView(Context con) {
        config.updateConfig(con);
        setBackground(config.getBG());
        invalidateAllKeys();

    }

    private void onDrawKeyBackground(Keyboard.Key myKey,
                                     Drawable keyBG,
                                     Canvas canvas) {
        if (keyBG != null) {
            int left = myKey.x + getPaddingLeft();
            int top = myKey.y + getPaddingTop();
            int right = left + myKey.width;
            int bottom = top + myKey.height;

            keyBG.setBounds(left, top, right, bottom);
            keyBG.setState(myKey.getCurrentDrawableState());
            keyBG.draw(canvas);
        }
    }

    private void onDrawLabel(
            Keyboard.Key myKey,
            Canvas canvas) {
        boolean b = myKey.label == null || myKey.label == "";
        if (!b) {
            float y1 = myKey.y + myKey.height / 2f - (mPaint.descent() + mPaint.ascent()) / 2f;
            float x1 = myKey.x + getPaddingLeft() + ((myKey.width / 2f));
            x1 -= mPaint.measureText(myKey.label.toString()) / 2f;
            canvas.drawText(myKey.label.toString(), x1, y1, mPaint);

        }
    }
}
