package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;

/**
 * Created by sanghoonkwon on 2018. 1. 11..
 */

public class HFNumberKeypadView extends LinearLayout {
    private TextView[] mTextViewNum;
    private ImageButton mImageButtonDel;

    private final int[] keyRes = {
            R.id.num0_textView,
            R.id.num1_textView,
            R.id.num2_textView,
            R.id.num3_textView,
            R.id.num4_textView,
            R.id.num5_textView,
            R.id.num6_textView,
            R.id.num7_textView,
            R.id.num8_textView,
            R.id.num9_textView};

    private OnNumberKeypadListener mOnNumberKeypadListener;

    public void setOnNumberKeypadListener(OnNumberKeypadListener onNumberListener) {
        this.mOnNumberKeypadListener = onNumberListener;
    }

    public HFNumberKeypadView(Context context) {
        super(context);
        initialize(context);
    }

    public HFNumberKeypadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public HFNumberKeypadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    private void initialize(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_number_keypad, this, true);

        mImageButtonDel = (ImageButton) findViewById(R.id.del_imageButton);
        mImageButtonDel.setOnClickListener(onClickListener);

        mTextViewNum = new TextView[keyRes.length];

        for (int i = 0; i < mTextViewNum.length; i++) {
            mTextViewNum[i] = (TextView) findViewById(keyRes[i]);
            mTextViewNum[i].setTag(i);
            mTextViewNum[i].setOnClickListener(onClickListener);
        }

        findViewById(R.id.empty_textView).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                testIndex += 1;
                testHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        testIndex = 0;

                    }
                }, 3000);
                if (testIndex == 10) {
                    if (mOnNumberKeypadListener != null)
                        mOnNumberKeypadListener.easterEgg();
                }
            }
        });
    }

    Handler testHandler = new Handler();
    int testIndex = 0;

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnNumberKeypadListener == null) return;
            switch (v.getId()) {
                case R.id.del_imageButton:
                    mOnNumberKeypadListener.onDeleteClicked();
                    break;
                default:
                    mOnNumberKeypadListener.onNumberClicked((int) v.getTag());
                    break;
            }

        }
    };

    /**************************************************
     * interface
     **************************************************/
    public interface OnNumberKeypadListener {
        void onNumberClicked(int num);

        void onDeleteClicked();

        void easterEgg();
    }
}
