package kr.co.hanwha.hifive.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;

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

public class HFWeekLoopDialog extends Dialog {

    private TextView mTextViewMon;
    private TextView mTextViewTue;
    private TextView mTextViewWed;
    private TextView mTextViewThu;
    private TextView mTextViewFri;

    private String mWeekLoop;

    private OnDialogCallback mCallback;

    public HFWeekLoopDialog(@NonNull Context context) {
        super(context);
    }

    public HFWeekLoopDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    protected HFWeekLoopDialog(@NonNull Context context, boolean cancelable, @Nullable DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public HFWeekLoopDialog(@NonNull Context context, @Nullable String weekLoop, @Nullable OnDialogCallback callback) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(true);
        this.mWeekLoop = weekLoop;
        this.mCallback = callback;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_hf_week_loop);

        mTextViewMon = (TextView) findViewById(R.id.mon_textView);
        mTextViewTue = (TextView) findViewById(R.id.tue_textView);
        mTextViewWed = (TextView) findViewById(R.id.wed_textView);
        mTextViewThu = (TextView) findViewById(R.id.thu_textView);
        mTextViewFri = (TextView) findViewById(R.id.fri_textView);

        mTextViewMon.setOnClickListener(onClickListener);
        mTextViewTue.setOnClickListener(onClickListener);
        mTextViewWed.setOnClickListener(onClickListener);
        mTextViewThu.setOnClickListener(onClickListener);
        mTextViewFri.setOnClickListener(onClickListener);

        findViewById(R.id.close_imageButton).setOnClickListener(onClickListener);
        findViewById(R.id.confirm_textView).setOnClickListener(onClickListener);

        //TODO - TEST
        final EditText editText = (EditText)findViewById(R.id.editText);
        editText.requestFocus();
        editText.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager input = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                input.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
            }
        }, 100);
        //TODO _ TEST END

        setWeekLoopButton();
        setWeekLoop();
    }

    private void setWeekLoop() {
        mWeekLoop = "0" +
                (mTextViewMon.isSelected() ? "1" : "0") +
                (mTextViewTue.isSelected() ? "1" : "0") +
                (mTextViewWed.isSelected() ? "1" : "0") +
                (mTextViewThu.isSelected() ? "1" : "0") +
                (mTextViewFri.isSelected() ? "1" : "0") +
                "0";
    }

    private void setWeekLoopButton() {
        if (TextUtils.isEmpty(mWeekLoop)) return;
        if (mWeekLoop.length() == 5)
            mWeekLoop = "0" + mWeekLoop + "0";
        mTextViewMon.setSelected("1".equals(mWeekLoop.substring(1, 2)));
        mTextViewTue.setSelected("1".equals(mWeekLoop.substring(2, 3)));
        mTextViewWed.setSelected("1".equals(mWeekLoop.substring(3, 4)));
        mTextViewThu.setSelected("1".equals(mWeekLoop.substring(4, 5)));
        mTextViewFri.setSelected("1".equals(mWeekLoop.substring(5, 6)));
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.mon_textView:
                    mTextViewMon.setSelected(!mTextViewMon.isSelected());
                    setWeekLoop();
                    break;
                case R.id.tue_textView:
                    mTextViewTue.setSelected(!mTextViewTue.isSelected());
                    setWeekLoop();
                    break;
                case R.id.wed_textView:
                    mTextViewWed.setSelected(!mTextViewWed.isSelected());
                    setWeekLoop();
                    break;
                case R.id.thu_textView:
                    mTextViewThu.setSelected(!mTextViewThu.isSelected());
                    setWeekLoop();
                    break;
                case R.id.fri_textView:
                    mTextViewFri.setSelected(!mTextViewFri.isSelected());
                    setWeekLoop();
                    break;
                case R.id.close_imageButton:
                    HFWeekLoopDialog.this.dismiss();
                    break;
                case R.id.confirm_textView:
                    if (mCallback != null) mCallback.onConfirm(mWeekLoop);
                    HFWeekLoopDialog.this.dismiss();
                    break;
            }
        }
    };

    /**************************************************
     * Callback
     **************************************************/
    public interface OnDialogCallback {
        void onConfirm(@NonNull String week_loop);
    }
}
