package com.exquisite.tools;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;

import com.exquisite.keyboard.MyKeyboard;

public class drawIcon {
    public static void onDrawKeyIcon(Keyboard.Key currentKey,
                                     Drawable drawKeyIcon,
                                     Canvas myCanvas,
                                     MyKeyboard myKeyboardView) {
        currentKey.icon = drawKeyIcon;
        currentKey.icon.setBounds(calculateIconBounds(currentKey, drawKeyIcon, myKeyboardView));
        currentKey.icon.draw(myCanvas);
    }

    private static Rect calculateIconBounds(Keyboard.Key currentKey,
                                            Drawable drawKeyIcon,
                                            MyKeyboard myKeyboardView) {
        float icon_w = drawKeyIcon.getIntrinsicWidth();
        float icon_h = drawKeyIcon.getIntrinsicHeight();
        float icon_wr = icon_w / currentKey.width;
        float icon_hr = icon_h / currentKey.height;

        float tep1, tep2;
        if (icon_wr > icon_hr) {
            tep2 = icon_wr;
            tep1 = Math.max(icon_wr, 0.5f);
        } else {
            tep2 = icon_hr;
            tep1 = Math.max(icon_hr, 0.5f);
        }

        icon_h = (icon_h / tep2) * tep1;
        icon_w = (icon_w / tep2) * tep1;

        int top = (int) (currentKey.y + myKeyboardView.getPaddingTop() + (currentKey.height - icon_h) / 2);
        int left = (int) (currentKey.x + myKeyboardView.getPaddingLeft() + (currentKey.width - icon_w) / 2);
        int bottom = top + (int) icon_h;
        int right = left + (int) icon_w;

        return new Rect(left, top, right, bottom);
    }
}
