package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.util.HFConstants;
import kr.co.hanwha.hifive.util.HFInfoUtil;

public class HFDepartmentPickerView extends RelativeLayout {
    private View mViewBackground;
    private LinearLayout mLayoutPicker;

    private int mHeight = 100;
    private String mSelectedValue = "";

    public HFDepartmentPickerView(Context context) {
        super(context);
        initialize(context, null);
    }

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

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

    private OnItemSelectedListener mOnItemSelectedListener;

    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        this.mOnItemSelectedListener = onItemSelectedListener;
    }

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        if (isInEditMode()) return;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_hf_department_picker, this, true);

        mViewBackground = (View) findViewById(R.id.background_view);
        mLayoutPicker = (LinearLayout) findViewById(R.id.picker_linearLayout);

        findViewById(R.id.cancel_textView).setOnClickListener(onClickListener);
        findViewById(R.id.background_view).setOnClickListener(onClickListener);
        findViewById(R.id.picker_linearLayout).setOnClickListener(onClickListener);
        findViewById(R.id.departmentLife_linearLayout).setOnClickListener(onClickListener);
        findViewById(R.id.departmentSnc_linearLayout).setOnClickListener(onClickListener);

        mLayoutPicker.measure(0, 0);
        mHeight = mLayoutPicker.getMeasuredHeight();
        mLayoutPicker.setTranslationY(mHeight);

        mSelectedValue = HFInfoUtil.getDepartmentValue(getContext());
        onItemSelected(mSelectedValue, false);
    }

    public void show() {
        this.setVisibility(View.VISIBLE);
        mViewBackground.setVisibility(View.VISIBLE);
        Animation alpha = new AlphaAnimation(0.0f, 1.0f);
        alpha.setDuration(200);
        mViewBackground.startAnimation(alpha);

        mLayoutPicker.animate().translationY(0);
    }

    private boolean isDismiss = false;

    public void dismiss() {
        if (isDismiss) return;
        isDismiss = true;

        Animation alpha = new AlphaAnimation(1.0f, 0.0f);
        alpha.setDuration(200);
        alpha.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mViewBackground.setVisibility(View.GONE);
                HFDepartmentPickerView.this.setVisibility(View.GONE);
                isDismiss = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mViewBackground.startAnimation(alpha);

        mLayoutPicker.animate().translationY(mHeight);
    }

    private void onItemSelected(@NonNull String value, boolean dismiss) {
        mSelectedValue = value;

        findViewById(R.id.departmentLife_linearLayout).setSelected(HFConstants.USER_TYPE_LF.equals(value));
        findViewById(R.id.departmentSnc_linearLayout).setSelected(HFConstants.USER_TYPE_SNC.equals(value));
        findViewById(R.id.departmentLife_imageButton).setVisibility(HFConstants.USER_TYPE_LF.equals(value) ? View.VISIBLE : View.GONE);
        findViewById(R.id.departmentSnc_imageButton).setVisibility(HFConstants.USER_TYPE_SNC.equals(value) ? View.VISIBLE : View.GONE);

        if (dismiss) {
            if (mOnItemSelectedListener != null) {
                HFInfoUtil.setDepartmentValue(getContext(), mSelectedValue);
                String text = HFInfoUtil.getDepartmentText(getContext());
                mOnItemSelectedListener.onItemSelected(text);
            }
            dismiss();
//            this.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    dismiss();
//                }
//            }, 500);

        }
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.background_view:
                    dismiss();
                    break;
                case R.id.cancel_textView:
                    dismiss();
                    break;
                case R.id.departmentSnc_linearLayout:
                    onItemSelected(HFConstants.USER_TYPE_SNC, true);
                    break;
                case R.id.departmentLife_linearLayout:
                    onItemSelected(HFConstants.USER_TYPE_LF, true);
                    break;
            }
        }
    };

    /**************************************************
     * OnItemSelectedListener - interface
     **************************************************/
    public interface OnItemSelectedListener {
        void onItemSelected(@NonNull String text);
    }
}