package org.commcare.android.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.StateSet;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.commcare.android.framework.ManagedUi;
import org.commcare.android.framework.UiElement;
import org.commcare.dalvik.R;

/**
 * Created by dancluna on 3/14/15.
 */
@ManagedUi(R.layout.square_button_text)
public class SquareButtonWithText extends RelativeLayout {
    @UiElement(R.id.square_button)
    SquareButton squareButton;

    @UiElement(R.id.text_view)
    TextView textView;

    Drawable backgroundImg;
    int backgroundColor = android.R.drawable.btn_default;
    String text = "";
    private int colorButtonText = R.color.cc_core_bg;

    //region Constructors

    public SquareButtonWithText(Context context) {
        super(context);
    }

    public SquareButtonWithText(Context context, AttributeSet attrs) {
        super(context, attrs);

        inflateAndExtractCustomParams(context, attrs);
    }

    public SquareButtonWithText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        inflateAndExtractCustomParams(context, attrs);
    }

    //endregion

    //region Custom parameter processing

    private void inflateAndExtractCustomParams(Context context, AttributeSet attrs) {
        inflate(context, R.layout.square_button_text, this);
        this.setClickable(true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SquareButtonWithText);

        backgroundImg = typedArray.getDrawable(R.styleable.SquareButtonWithText_img);
        backgroundColor = getResources().getColor(typedArray.getResourceId(R.styleable.SquareButtonWithText_backgroundcolor, android.R.color.transparent));
        text = typedArray.getString(R.styleable.SquareButtonWithText_subtitle);
        colorButtonText = typedArray.getResourceId(R.styleable.SquareButtonWithText_colorText, colorButtonText);

        typedArray.recycle();

        squareButton = (SquareButton) findViewById(R.id.square_button);
        textView = (TextView) findViewById(R.id.text_view);

        if (isInEditMode()) {
            setUI(R.color.cc_brand_color, getResources().getDrawable(R.drawable.barcode), "Your text goes here", colorButtonText);
        }

        setUI(backgroundColor, backgroundImg, text, colorButtonText);
    }

    private void setUI(int backgroundColor, Drawable backgroundImg, String text, int colorButtonText) {
        setColor(backgroundColor);
        setImage(backgroundImg);
        setText(text);
        setTextColor(colorButtonText);
    }

    //endregion

    //region Compatibility methods

    public void setText(String text) {
        if (textView != null) {
            textView.setText(text);
        }
    }


    public void setImage(Drawable backgroundImg) {
        squareButton.setImageDrawable(backgroundImg);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setColor(int backgroundColor) {
        ColorDrawable colorDrawable = new ColorDrawable(backgroundColor);

        int color = ViewUtil.getColorDrawableColor(colorDrawable);

        float[] hsvOutput = new float[3];
        Color.colorToHSV(color, hsvOutput);

        hsvOutput[2] = (float) (hsvOutput[2] / 1.5);

        int selectedColor = Color.HSVToColor(hsvOutput);

        ColorDrawable pressedBackground = new ColorDrawable(selectedColor);

        StateListDrawable sld = new StateListDrawable();
        sld.addState(new int[]{android.R.attr.state_pressed}, pressedBackground);
        sld.addState(StateSet.WILD_CARD, colorDrawable);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            this.setBackground(sld);
        } else {
            this.setBackgroundDrawable(sld);
        }

    }

    public void setTextColor(int textColor) {
        textView.setTextColor(getResources().getColor(textColor));
    }

    //endregion
}

@@ -495,356 +495,158 @@
 are.
-android.framework.ManagedUi;%0Aimport org.commcare.android.framework.UiElement;%0Aimport org.commcare.dalvik.R;%0A%0A/**%0A * Created by dancluna on 3/14/15.%0A */%0A@ManagedUi(R.layout.square_button_text)%0Apublic class SquareButtonWithText extends RelativeLayout %7B%0A    @UiElement(R.id.square_button)%0A    SquareButton squareButton;%0A%0A    @UiElement(R.id.text_view)
+dalvik.R;%0A%0A/**%0A * @author Daniel Luna (dluna@dimagi.com)%0A */%0Apublic class SquareButtonWithText extends RelativeLayout %7B%0A    SquareButton squareButton;
 %0A   
@@ -833,35 +833,8 @@
 g;%0A%0A
-    //region Constructors%0A%0A
     
@@ -1277,67 +1277,8 @@
  %7D%0A%0A
-    //endregion%0A%0A    //region Custom parameter processing%0A%0A
     
@@ -2654,61 +2654,8 @@
  %7D%0A%0A
-    //endregion%0A%0A    //region Compatibility methods%0A%0A
     
@@ -2774,17 +2774,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -3772,25 +3772,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    p
@@ -3905,24 +3905,7 @@
    %7D
-%0A%0A    //endregion
 %0A%7D%0A

Remove ManagedUi/UiElement from SquareButtonWithText