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.widget.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.TaskDetailData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.enums.TaskFlagType;
import kr.co.hanwha.hifive.enums.TaskStatusType;

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

public class HFTaskPopupMenuView extends LinearLayout {
    private TextView mTextViewEdit;
    private TextView mTextViewDelete;
    private TextView mTextViewAccept;
    private TextView mTextViewReject;
    private TextView mTextViewEvalConfirm;
    private TextView mTextViewEvalReject;
    private TextView mTextViewReRequest;
    private TextView mTextViewDeleteRequest;

    private OnTaskPopupMenuListener mOnTaskPopupMenuListener;

    public void setOnTaskPopupMenuListener(OnTaskPopupMenuListener onTaskPopupMenuListener) {
        this.mOnTaskPopupMenuListener = onTaskPopupMenuListener;
    }

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

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

    public HFTaskPopupMenuView(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_task_popup_menu, this, true);

        if (isInEditMode()) return;

        mTextViewEdit = (TextView) findViewById(R.id.edit_textView);
        mTextViewDelete = (TextView) findViewById(R.id.delete_textView);
        mTextViewAccept = (TextView) findViewById(R.id.accept_textView);
        mTextViewReject = (TextView) findViewById(R.id.reject_textView);
        mTextViewEvalConfirm = (TextView) findViewById(R.id.evalConfirm_textView);
        mTextViewEvalReject = (TextView) findViewById(R.id.evalReject_textView);
        mTextViewReRequest = (TextView) findViewById(R.id.reRequest_textView);
        mTextViewDeleteRequest = (TextView) findViewById(R.id.deleteRequest_textView);

        mTextViewEdit.setOnClickListener(onClickListener);
        mTextViewDelete.setOnClickListener(onClickListener);
        mTextViewAccept.setOnClickListener(onClickListener);
        mTextViewReject.setOnClickListener(onClickListener);
        mTextViewEvalConfirm.setOnClickListener(onClickListener);
        mTextViewEvalReject.setOnClickListener(onClickListener);
        mTextViewReRequest.setOnClickListener(onClickListener);
        mTextViewDeleteRequest.setOnClickListener(onClickListener);
        findViewById(R.id.containerBg_relativeLayout).setOnClickListener(onClickListener);
    }

    public void setType(int flag, @NonNull TaskDetailData data) {
        findViewById(R.id.underline_view).setVisibility(View.GONE);
        mTextViewEdit.setVisibility(View.GONE);
        mTextViewDelete.setVisibility(View.GONE);
        mTextViewAccept.setVisibility(View.GONE);
        mTextViewReject.setVisibility(View.GONE);
        mTextViewReRequest.setVisibility(View.GONE);
        mTextViewDeleteRequest.setVisibility(View.GONE);
        mTextViewEvalConfirm.setVisibility(View.GONE);
        mTextViewEvalReject.setVisibility(View.GONE);

        boolean isAuthor = MemberDataRepository.getInstance().isMember(data.getAuthor_member_id());
        boolean isReceiver = MemberDataRepository.getInstance().isMember(data.getMember_id());

        if (data.getStatus() == TaskStatusType.ASSIGNED.getCode()) {
            if (isReceiver) mTextViewEdit.setVisibility(View.VISIBLE);
            if (isReceiver && isAuthor) mTextViewDelete.setVisibility(View.VISIBLE);
        } else if (data.getStatus() == TaskStatusType.REQUESTED.getCode()) {
            if (isAuthor) mTextViewEdit.setVisibility(View.VISIBLE);
            if (isReceiver) {
                mTextViewAccept.setVisibility(View.VISIBLE);
                mTextViewReject.setVisibility(View.VISIBLE);
            }
        } else if (data.getStatus() == TaskStatusType.REJECTED_REQUEST.getCode()) {
            if (isAuthor) {
                mTextViewEdit.setVisibility(View.VISIBLE);
                findViewById(R.id.underline_view).setVisibility(View.VISIBLE);
                mTextViewReRequest.setVisibility(View.VISIBLE);
                mTextViewDeleteRequest.setVisibility(View.VISIBLE);
            }
        } else if (data.getStatus() == TaskStatusType.DOING_TODAY.getCode()) {
            if (isReceiver) mTextViewEdit.setVisibility(View.VISIBLE);
            if (isReceiver && isAuthor) mTextViewDelete.setVisibility(View.VISIBLE);
        } else if (data.getStatus() == TaskStatusType.EVALUATED.getCode()) {
            if (isAuthor) {
                mTextViewEvalConfirm.setVisibility(View.VISIBLE);
                mTextViewEvalReject.setVisibility(View.VISIBLE);
            }
        }
        /*
        if (isReceiver) {
            if (data.getStatus() == TaskStatusType.ASSIGNED.getCode() ||
                    data.getStatus() == TaskStatusType.DOING_TODAY.getCode()) {
                mTextViewEdit.setVisibility(View.VISIBLE);
            }
        }
        if (isAuthor) {
            if (data.getStatus() == TaskStatusType.REQUESTED.getCode() ||
                    data.getStatus() == TaskStatusType.REJECTED_REQUEST.getCode()) {
                mTextViewEdit.setVisibility(View.VISIBLE);
            }
        }

        if (isReceiver && isAuthor &&
                data.getStatus() == TaskStatusType.ASSIGNED.getCode()) {
            mTextViewDelete.setVisibility(View.VISIBLE);
        }

        if (TaskFlagType.NONE.getCode() == flag) {

        } else if (TaskFlagType.ASSIGNED_MY_TASK.getCode() == flag) {
            findViewById(R.id.underline_view).setVisibility(View.VISIBLE);
        } else if (TaskFlagType.WAITING_TO_ME.getCode() == flag) {
            mTextViewAccept.setVisibility(View.VISIBLE);
            mTextViewReject.setVisibility(View.VISIBLE);
        } else if (TaskFlagType.COMPLETED_MY_TASK.getCode() == flag) {

        } else if (TaskFlagType.REQUEST_TASK_REJECTED.getCode() == flag) {
            findViewById(R.id.underline_view).setVisibility(View.VISIBLE);
            mTextViewReRequest.setVisibility(View.VISIBLE);
            mTextViewDeleteRequest.setVisibility(View.VISIBLE);
        } else if (TaskFlagType.REQUEST_TASK_EVALUATION.getCode() == flag) {
            findViewById(R.id.underline_view).setVisibility(View.VISIBLE);
            mTextViewEvalConfirm.setVisibility(View.VISIBLE);
            mTextViewEvalReject.setVisibility(View.VISIBLE);
        } else if (TaskFlagType.REQUEST_TASK_ASSIGNED.getCode() == flag &&
                TaskStatusType.REJECTED_REQUEST.getCode() == data.getStatus()) {

        }
        */
    }

    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnTaskPopupMenuListener != null) {
                switch (v.getId()) {
                    case R.id.containerBg_relativeLayout:
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                    case R.id.edit_textView:
                        mOnTaskPopupMenuListener.doEditTask();
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                    case R.id.delete_textView:
                        mOnTaskPopupMenuListener.doDeleteTask();
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                    case R.id.accept_textView:
                        mOnTaskPopupMenuListener.doAccept();
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                    case R.id.reject_textView:
                        mOnTaskPopupMenuListener.doReject();
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                    case R.id.evalConfirm_textView:
                        mOnTaskPopupMenuListener.doEvaluationAccept();
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                    case R.id.evalReject_textView:
                        mOnTaskPopupMenuListener.doEvaluationReject();
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                    case R.id.reRequest_textView:
                        mOnTaskPopupMenuListener.doReRequest();
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                    case R.id.deleteRequest_textView:
                        mOnTaskPopupMenuListener.doDeleteRequest();
                        mOnTaskPopupMenuListener.dismiss();
                        break;
                }
            }
        }
    };

    /**************************************************
     * OnCommentEditListener
     **************************************************/
    public interface OnTaskPopupMenuListener {
        /**
         * 팦업 닫기
         */
        void dismiss();

        /**
         * Task 편집
         */
        void doEditTask();

        /**
         * Task 삭제
         */
        void doDeleteTask();

        /**
         * TASK 수락
         */
        void doAccept();

        /**
         * TASK 반려
         */
        void doReject();

        /**
         * 요청 삭제
         */
        void doDeleteRequest();

        /**
         * 재 요청
         */
        void doReRequest();

        /**
         * 평가 수락
         */
        void doEvaluationAccept();

        /**
         * 평가 반려
         */
        void doEvaluationReject();
    }
}
