package kr.co.hanwha.hifive.dialog;

import android.app.Dialog;
import android.content.Context;
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.widget.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.enums.ProjectEvaluationType;

/**
 * Created by sanghoonkwon on 2018. 2. 22..
 */

public class HFProjectColorDialog extends Dialog {
    private ProjectEvaluationType mType = ProjectEvaluationType.NONE;
    private TextView mTextViewTitle;
    private LinearLayout mLayoutGreen;
    private LinearLayout mLayoutYellow;
    private LinearLayout mLayoutYellowDouble;
    private LinearLayout mLayoutRed;
    private String mTitle = "";
    private int mEvaluation;
    private OnDialogCallback mCallback;

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

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

    public HFProjectColorDialog(@NonNull Context context, @Nullable String title, @Nullable int evaluation, @Nullable OnDialogCallback callback) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(true);
        this.mTitle = title;
        this.mEvaluation = evaluation;
        this.mCallback = callback;
    }


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

        setContentView(R.layout.dialog_hf_project_color);

        mTextViewTitle = (TextView) findViewById(R.id.title_textView);
        mLayoutGreen = (LinearLayout) findViewById(R.id.green_linearLayout);
        mLayoutYellow = (LinearLayout) findViewById(R.id.yellow_linearLayout);
        mLayoutYellowDouble = (LinearLayout) findViewById(R.id.yellowDouble_linearLayout);
        mLayoutRed = (LinearLayout) findViewById(R.id.red_linearLayout);

        findViewById(R.id.negative_textView).setOnClickListener(onClickListener);
        findViewById(R.id.positive_textView).setOnClickListener(onClickListener);

        if (TextUtils.isEmpty(mTitle) == false)
            mTextViewTitle.setText(mTitle);

        mLayoutGreen.setOnClickListener(onClickListener);
        mLayoutYellow.setOnClickListener(onClickListener);
        mLayoutYellowDouble.setOnClickListener(onClickListener);
        mLayoutRed.setOnClickListener(onClickListener);

        if (mEvaluation == ProjectEvaluationType.GREEN.getCode())
            mType = ProjectEvaluationType.GREEN;
        else if (mEvaluation == ProjectEvaluationType.YELLOW.getCode())
            mType = ProjectEvaluationType.YELLOW;
        else if (mEvaluation == ProjectEvaluationType.YELLOW_DOUBLE.getCode())
            mType = ProjectEvaluationType.YELLOW_DOUBLE;
        else if (mEvaluation == ProjectEvaluationType.RED.getCode())
            mType = ProjectEvaluationType.RED;
        else mType = ProjectEvaluationType.NONE;
        setType(mType);
    }

    private void setType(@NonNull ProjectEvaluationType type) {
        mType = type;

        mLayoutGreen.setSelected(type == ProjectEvaluationType.GREEN);
        mLayoutYellow.setSelected(type == ProjectEvaluationType.YELLOW);
        mLayoutYellowDouble.setSelected(type == ProjectEvaluationType.YELLOW_DOUBLE);
        mLayoutRed.setSelected(type == ProjectEvaluationType.RED);
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.negative_textView:
                    HFProjectColorDialog.this.dismiss();
                    break;
                case R.id.positive_textView: {
                    if (mCallback != null) mCallback.onConfirm(mType);
                    HFProjectColorDialog.this.dismiss();
                    break;
                }
                case R.id.green_linearLayout:
                    setType(ProjectEvaluationType.GREEN);
                    break;
                case R.id.yellow_linearLayout:
                    setType(ProjectEvaluationType.YELLOW);
                    break;
                case R.id.yellowDouble_linearLayout:
                    setType(ProjectEvaluationType.YELLOW_DOUBLE);
                    break;
                case R.id.red_linearLayout:
                    setType(ProjectEvaluationType.RED);
                    break;
            }
        }
    };

    /**************************************************
     * Callback
     **************************************************/
    public interface OnDialogCallback {

        /**
         * 완료 - 확인
         */
        void onConfirm(ProjectEvaluationType type);

    }
}