package kr.co.hanwha.hifive.adapter.viewholder;

import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.ProjectSpinnerAdapter;
import kr.co.hanwha.hifive.adapter.contract.MainProjectListContract;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.ProjectCounterMeasuresInfo;
import kr.co.hanwha.hifive.data.model.ProjectInfo;
import kr.co.hanwha.hifive.data.model.ProjectMembersInfo;
import kr.co.hanwha.hifive.data.model.SpinnerData;
import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.enums.MemberRoleType;
import kr.co.hanwha.hifive.enums.ProjectEvaluationType;
import kr.co.hanwha.hifive.enums.ProjectFilterType;
import kr.co.hanwha.hifive.util.HFTaskUtil;
import kr.co.hanwha.hifive.view.HFProjectIssueView;

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

public class MainProjectHeaderViewHolder extends BaseViewHolder {
    private Spinner mSpinnerProject;
    private ProjectSpinnerAdapter mAdapterProject;

    private TextView mTextViewProject;
    private TextView mTextViewMember;
    private TextView mTextViewTitle;
    private TextView mTextViewSubTitle;
    private View mViewStatus;
    private View mViewStatus2;
    private View mViewStatus3;
    private TextView mTextViewColor;

    private LinearLayout mLayoutIssueTop;
    private LinearLayout mLayoutIssueBottom;
    private LinearLayout mLayoutContainer;
    private LinearLayout mLayoutIssueAdd;

    private LinearLayout mLayoutFilterInProgress;
    private ImageView mImageViewFilterInProgress;
    private TextView mTextViewFilterInProgress;
    private LinearLayout mLayoutFilterAll;
    private ImageView mImageViewFilterAll;
    private TextView mTextViewFilterAll;

    private int mProject_id = 0;
    private ArrayList<SpinnerData> mListProjects;
    private ArrayList<ProjectMembersInfo> mListProjectMembersInfo;
    private ArrayList<ProjectCounterMeasuresInfo> mListProjectCounterMeasures;

    private MainProjectListContract.OnMainProjectListListener mOnMainProjectListListener;

    public MainProjectHeaderViewHolder(@NonNull ViewGroup viewGroup, @Nullable MainProjectListContract.OnMainProjectListListener onMainProjectListListener) {

        super(viewGroup, R.layout.item_main_project_header);

        mAdapterProject = new ProjectSpinnerAdapter(getContext());

        mSpinnerProject = (Spinner) itemView.findViewById(R.id.project_spinner);
        mSpinnerProject.setAdapter(mAdapterProject);
        mSpinnerProject.setOnItemSelectedListener(onItemSelectedListener);

        mTextViewProject = (TextView) itemView.findViewById(R.id.project_textView);
        mTextViewMember = (TextView) itemView.findViewById(R.id.member_textView);
        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);
        mTextViewSubTitle = (TextView) itemView.findViewById(R.id.subTitle_textView);
        mViewStatus = (View) itemView.findViewById(R.id.status_view);
        mViewStatus2 = (View) itemView.findViewById(R.id.status2_view);
        mViewStatus3 = (View) itemView.findViewById(R.id.status3_view);
        mTextViewColor = (TextView) itemView.findViewById(R.id.color_textView);

        mLayoutIssueTop = (LinearLayout) itemView.findViewById(R.id.issueTop_linearLayout);
        mLayoutIssueBottom = (LinearLayout) itemView.findViewById(R.id.issueBottom_linearLayout);
        mLayoutContainer = (LinearLayout) itemView.findViewById(R.id.container_linearLayout);
        mLayoutIssueAdd = (LinearLayout) itemView.findViewById(R.id.issueAdd_linearLayout);

        mLayoutFilterInProgress = (LinearLayout) itemView.findViewById(R.id.filterInProgress_linearLayout);
        mImageViewFilterInProgress = (ImageView) itemView.findViewById(R.id.filterInProgress_imageView);
        mTextViewFilterInProgress = (TextView) itemView.findViewById(R.id.filterInProgress_textView);
        mLayoutFilterAll = (LinearLayout) itemView.findViewById(R.id.filterAll_linearLayout);
        mImageViewFilterAll = (ImageView) itemView.findViewById(R.id.filterAll_imageView);
        mTextViewFilterAll = (TextView) itemView.findViewById(R.id.filterAll_textView);

        mOnMainProjectListListener = onMainProjectListListener;

        onFilterClicked(ProjectFilterType.IN_PROGRESS);
    }

    public void setListProjectMembersInfo(ArrayList<ProjectMembersInfo> listProjectMembersInfo) {
        this.mListProjectMembersInfo = listProjectMembersInfo;
    }

    public void setListProjectCounterMeasures(ArrayList<ProjectCounterMeasuresInfo> listProjectCounterMeasures) {
        this.mListProjectCounterMeasures = listProjectCounterMeasures;
    }

    public void setListProjects(ArrayList<SpinnerData> listProjects) {
        this.mListProjects = listProjects;
    }

    public void setProject_id(int project_id) {
        this.mProject_id = project_id;
    }

    @Override
    public void onBind(Object object, int index) {
        super.onBind(object, index);

        mLayoutContainer.removeAllViews();

        if (mListProjects != null) {
            mAdapterProject.addItems(mListProjects);
            mAdapterProject.setSelectedProjectid(mProject_id);
            mAdapterProject.notifyDataSetChanged();

            if (mListProjects.size() > 1) {
                mSpinnerProject.setVisibility(View.VISIBLE);
                mTextViewProject.setVisibility(View.GONE);
            } else {
                mSpinnerProject.setVisibility(View.GONE);
                mTextViewProject.setVisibility(View.VISIBLE);
            }
        } else {
            mSpinnerProject.setVisibility(View.GONE);
            mTextViewProject.setVisibility(View.VISIBLE);
        }

        if (object != null && object instanceof ProjectInfo) {
            ProjectInfo data = (ProjectInfo) object;
            mTextViewProject.setText(data.getProject_name());

            String captain = "";
            int count = 0;
            int captainCount = 0;
            if (mListProjectMembersInfo != null && mListProjectMembersInfo.size() > 0) {
                for (ProjectMembersInfo member : mListProjectMembersInfo) {
                    if (member.getRole_code() == MemberRoleType.UNIT_LEADER.getCode()) {
                        captain += (TextUtils.isEmpty(captain) ? "" : ", ") + member.getMember_name();
                        captainCount += 1;
                    }
                }
                count = mListProjectMembersInfo.size() - captainCount;
                if (count > 0)
                    mTextViewMember.setText(String.format(getContext().getString(R.string.main_project_member_format), captain, count));
                else
                    mTextViewMember.setText(captain);
            } else {
                mTextViewMember.setText("");
            }

            if (data.getCmdr_intent() != null && data.getCmdr_intent().size() > 0) {
                if (TextUtils.isEmpty(data.getCmdr_intent().get(0)) == false) {
                    mTextViewTitle.setText(data.getCmdr_intent().get(0));
                    mTextViewTitle.setVisibility(View.VISIBLE);
                } else {
                    mTextViewTitle.setText("");
                    mTextViewTitle.setVisibility(View.GONE);
                }
                if (data.getCmdr_intent() != null && data.getCmdr_intent().size() > 1) {
                    if (TextUtils.isEmpty(data.getCmdr_intent().get(1)) == false) {
                        mTextViewSubTitle.setText(data.getCmdr_intent().get(1));
                        mTextViewSubTitle.setVisibility(View.VISIBLE);
                    } else {
                        mTextViewSubTitle.setText("");
                        mTextViewSubTitle.setVisibility(View.GONE);
                    }
                } else {
                    mTextViewSubTitle.setText("");
                    mTextViewSubTitle.setVisibility(View.GONE);
                }
            } else {
                mTextViewTitle.setText("");
                mTextViewTitle.setVisibility(View.GONE);

                mTextViewSubTitle.setText("");
                mTextViewSubTitle.setVisibility(View.GONE);
            }

            //Status
            Drawable status = HFTaskUtil.getProjectStatusBackground(getContext(), data.getEvaluation());
            if (status == null) {
                mViewStatus.setVisibility(View.GONE);
                mViewStatus2.setVisibility(View.GONE);
                mViewStatus3.setVisibility(View.GONE);
            } else {
                mViewStatus.setBackground(status);
                mViewStatus.setVisibility(View.VISIBLE);
                if (data.getEvaluation() == ProjectEvaluationType.YELLOW_DOUBLE.getCode()) {
                    mViewStatus.setVisibility(View.GONE);
                    mViewStatus2.setVisibility(View.VISIBLE);
                    mViewStatus3.setVisibility(View.VISIBLE);
                } else {
                    mViewStatus.setVisibility(View.VISIBLE);
                    mViewStatus2.setVisibility(View.GONE);
                    mViewStatus3.setVisibility(View.GONE);
                }
            }

            Drawable issueTopBg = HFTaskUtil.getProjectIssueTopBackground(getContext(), data.getEvaluation());
            Drawable issueBottomBg = HFTaskUtil.getProjectIssueBottomBackground(getContext(), data.getEvaluation());
            mLayoutIssueTop.setBackground(issueTopBg);
            mLayoutIssueBottom.setBackground(issueBottomBg);

            if (mListProjectCounterMeasures != null && mListProjectCounterMeasures.size() > 0) {
                for (int i = 0; i < mListProjectCounterMeasures.size(); i++) {
                    HFProjectIssueView view = new HFProjectIssueView(getContext());
                    view.setIssue(mListProjectCounterMeasures.get(i));
                    mLayoutContainer.addView(view);
                }
            } else {
                HFProjectIssueView view = new HFProjectIssueView(getContext());
                view.setMessage(getContext().getString(R.string.main_project_issue_empty));
                mLayoutContainer.addView(view);
            }

            boolean isCaptain = false;
            if (mListProjectMembersInfo != null) {
                for (ProjectMembersInfo member : mListProjectMembersInfo) {
                    if (member.getRole_code() == MemberRoleType.UNIT_LEADER.getCode() &&
                            member.getMember_id() == MemberDataRepository.getInstance().getMemberId())
                        isCaptain = true;
                }
            }

            // Captain 고유 기능
            if (isCaptain) {
                mTextViewColor.setVisibility(View.VISIBLE);
                mLayoutIssueAdd.setVisibility(View.VISIBLE);
                if (mOnMainProjectListListener != null) {
                    mTextViewColor.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnMainProjectListListener.onProjectColorClicked();
                        }
                    });

                    mLayoutIssueAdd.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnMainProjectListListener.onEditIssueClicked();
                        }
                    });
                }
            } else {
                mTextViewColor.setVisibility(View.GONE);
                mLayoutIssueAdd.setVisibility(View.GONE);
                mTextViewColor.setOnClickListener(null);
                mLayoutIssueAdd.setOnClickListener(null);
            }

            // Filter
            if (mOnMainProjectListListener != null) {
                mLayoutFilterInProgress.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onFilterClicked(ProjectFilterType.IN_PROGRESS);
                        mOnMainProjectListListener.onFilterClicked(ProjectFilterType.IN_PROGRESS);
                    }
                });
                mLayoutFilterAll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onFilterClicked(ProjectFilterType.ALL);
                        mOnMainProjectListListener.onFilterClicked(ProjectFilterType.ALL);
                    }
                });
            }
        }
    }

    private void onFilterClicked(ProjectFilterType filterType) {
        if (filterType == ProjectFilterType.IN_PROGRESS) {
            mImageViewFilterInProgress.setVisibility(View.VISIBLE);
            mImageViewFilterAll.setVisibility(View.INVISIBLE);
            mTextViewFilterInProgress.setSelected(true);
            mTextViewFilterAll.setSelected(false);
        } else if (filterType == ProjectFilterType.ALL) {
            mImageViewFilterInProgress.setVisibility(View.INVISIBLE);
            mImageViewFilterAll.setVisibility(View.VISIBLE);
            mTextViewFilterInProgress.setSelected(false);
            mTextViewFilterAll.setSelected(true);
        }
    }

    /**************************************************
     * AdapterView.OnItemSelectedListener
     **************************************************/
    AdapterView.OnItemSelectedListener onItemSelectedListener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (mAdapterProject == null) return;

            int project_id = mAdapterProject.getProjectId(position);
            if (mOnMainProjectListListener != null)
                mOnMainProjectListListener.onProjectItemSelected(project_id);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
}
