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.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

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

/**
 * Created by sanghoonkwon on 2018. 3. 14..
 */

public class HFEvaluationDialog extends Dialog {
    private ImageView[] mImageViewStars;
    private int[] res_stars = {R.id.star1_imageView, R.id.star2_imageView, R.id.star3_imageView};

    private TextView mTextViewPositive;
    private TextView mTextViewNegative;

    private OnDialogCallback mCallback;

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

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

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

    public HFEvaluationDialog(@NonNull Context context, @Nullable OnDialogCallback callback) {
        super(context, R.style.DialogFadeAnim);
        setCancelable(false);
        this.mCallback = callback;
    }

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

        setContentView(R.layout.dialog_hf_evaluation);

        mImageViewStars = new ImageView[res_stars.length];
        for (int i = 0; i < res_stars.length; i++) {
            mImageViewStars[i] = (ImageView) findViewById(res_stars[i]);
            mImageViewStars[i].setTag(i);
//            mImageViewStars[i].setOnClickListener(onStarClickListener);
            mImageViewStars[i].setOnTouchListener(onTouchListener);
        }

        mTextViewPositive = (TextView) findViewById(R.id.positive_textView);
        mTextViewNegative = (TextView) findViewById(R.id.negative_textView);

        mTextViewPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TaskEvaluationType type = TaskEvaluationType.NONE;
                if (mImageViewStars[2].isSelected()) type = TaskEvaluationType.STAR_THREE;
                else if (mImageViewStars[1].isSelected()) type = TaskEvaluationType.STAR_TWO;
                else if (mImageViewStars[0].isSelected()) type = TaskEvaluationType.STAR_ONE;

                if (mCallback != null && type != TaskEvaluationType.NONE) {
                    mCallback.onPositive(type);
                    HFEvaluationDialog.this.dismiss();
                }
            }
        });

        mTextViewNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HFEvaluationDialog.this.dismiss();
            }
        });
    }

    /**************************************************
     * View.OnTouchListener
     **************************************************/
    View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    int index = (int) v.getTag();
                    for (int i = 0; i < res_stars.length; i++) {
                        mImageViewStars[i].setSelected(index >= i);
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
//                    Log.d("", mImageViewStars[0].getX() + " , " + mImageViewStars[0].getY()
//                            + " : " + event.getX() + " , " + event.getY());
                    break;
            }
            return true;
        }
    };

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onStarClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int index = (int) v.getTag();
            for (int i = 0; i < res_stars.length; i++) {
                mImageViewStars[i].setSelected(index >= i);
            }
        }
    };

    /**************************************************
     * Callback
     **************************************************/
    public interface OnDialogCallback {
        void onPositive(@NonNull TaskEvaluationType type);
    }
}
