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

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.HFApplication;
import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.MainDashboardListContract;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.DabProjectData;
import kr.co.hanwha.hifive.enums.ProjectEvaluationType;
import kr.co.hanwha.hifive.util.HFCommonUtil;

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

public class MainDashboardGridViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutBody;
    private View mViewStatus;
    private View mViewStatus2;
    private TextView mTextViewProject;
    private TextView mTextViewMember;
    private TextView mTextViewLog;

    private MainDashboardListContract.OnMainDashboardListListener mOnMainDashboardListListener;

    public MainDashboardGridViewHolder(@NonNull ViewGroup viewGroup,
                                       MainDashboardListContract.OnMainDashboardListListener onMainDashboardListListener) {
        super(viewGroup, R.layout.item_main_dashboard_grid);

        mOnMainDashboardListListener = onMainDashboardListListener;

        mLayoutBody = (LinearLayout) itemView.findViewById(R.id.body_linearLayout);
        mViewStatus = (View) itemView.findViewById(R.id.status_view);
        mViewStatus2 = (View) itemView.findViewById(R.id.status2_view);
        mTextViewProject = (TextView) itemView.findViewById(R.id.project_textView);
        mTextViewMember = (TextView) itemView.findViewById(R.id.member_textView);
        mTextViewLog = (TextView) itemView.findViewById(R.id.log_textView);
    }

    @Override
    public void onBind(@NonNull Object object, @NonNull final int cellWidth) {
        super.onBind(object, cellWidth);

        if (cellWidth > 0) {
            int dp9_5 = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp9_5);
            int dp10_5 = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp10_5);
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(cellWidth, cellWidth);
            param.leftMargin = dp10_5;
            param.rightMargin = dp10_5;
            param.topMargin = 0;
            param.bottomMargin = dp9_5;
            mLayoutBody.setLayoutParams(param);
        }

        if (object != null && object instanceof DabProjectData) {
            final DabProjectData data = (DabProjectData) object;

            mTextViewProject.setText(TextUtils.isEmpty(data.getProject_name()) ? "" : data.getProject_name());

            String memberStr = "";
            if (data.getListCaptains() != null && data.getListCaptains().size() > 0) {
                for (int i = 0; i < data.getListCaptains().size(); i++) {
                    memberStr += (i == 0 ? "" : ", ") + data.getListCaptains().get(i).getMember_name();
                }
            }
            mTextViewMember.setText(memberStr);

            if (data.getEvaluation() == ProjectEvaluationType.RED.getCode()) {
                mViewStatus.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_grid_status_red));
                mViewStatus2.setVisibility(View.GONE);
                mLayoutBody.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_evaluation_red));
            } else if (data.getEvaluation() == ProjectEvaluationType.YELLOW.getCode()) {
                mViewStatus.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_grid_status_yellow));
                mViewStatus2.setVisibility(View.GONE);
                mLayoutBody.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_evaluation_yellow));
            } else if (data.getEvaluation() == ProjectEvaluationType.YELLOW_DOUBLE.getCode()) {
                mViewStatus.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_grid_status_yellow));
                mViewStatus2.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_grid_status_yellow));
                mViewStatus2.setVisibility(View.VISIBLE);
                mLayoutBody.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_evaluation_yellow));
            } else {
                mViewStatus.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_grid_status_green));
                mViewStatus2.setVisibility(View.GONE);
                mLayoutBody.setBackground(getContext().getResources().getDrawable(R.drawable.circle_project_evaluation_green));
            }

            if (mOnMainDashboardListListener != null) {
                mLayoutBody.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnMainDashboardListListener.onItemClicked(data);
                    }
                });
            }

            // Log
            if (HFApplication.DEV_LOG) {
                String log = "Project Id : " + data.getProject_id();
                String captainStr = "";
                if (data.getListCaptains() != null && data.getListCaptains().size() > 0) {
                    log += "\nCaptain : ";
                    for (int i = 0; i < data.getListCaptains().size(); i++) {
                        log += (i == 0 ? "" : ", ") + data.getListCaptains().get(i).getMember_id()
                                + "(" + data.getListCaptains().get(i).getRole_code() + ")";
                    }
                }

                mTextViewLog.setText(log);
                mTextViewLog.setVisibility(View.VISIBLE);
            } else {
                mTextViewLog.setVisibility(View.GONE);
            }
        }
    }
}