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.ImageView;
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.ProjectAchievementType;
import kr.co.hanwha.hifive.util.HFLogUtil;

/**
 * Created by aaron on 2018. 8. 8.
 */

public class MainDashboardListViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutBody;
    private View mViewStatus;
    private TextView mTextViewProject;
    private TextView mTextViewMember;
    private TextView mTextViewUnit;
    private TextView mTextViewLog;

    private MainDashboardListContract.OnMainDashboardListListener mOnMainDashboardListListener;

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

        mOnMainDashboardListListener = onMainDashboardListListener;
        viewInit();
    }

    private void viewInit() {
        mLayoutBody = (LinearLayout) itemView.findViewById(R.id.body_linearLayout);
        mViewStatus = (ImageView) itemView.findViewById(R.id.status_view);
        mTextViewProject = (TextView) itemView.findViewById(R.id.project_textView);
        mTextViewMember = (TextView) itemView.findViewById(R.id.member_textView);
        mTextViewUnit = (TextView) itemView.findViewById(R.id.unit_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 (object != null && object instanceof DabProjectData) {
            final DabProjectData data = (DabProjectData) object;

            mTextViewProject.setText(TextUtils.isEmpty(data.getProject_name()) ? "" : data.getProject_name());
            mTextViewMember.setText(makeMemberStr(data));
            mTextViewUnit.setText(data.getTrack_name());

            ProjectAchievementType achieveType = ProjectAchievementType.valueOf(data.getProject_avg_achieve_rate());
            switch (achieveType) {
                case GREEN:
                    mViewStatus.setBackground(getContext().getResources().getDrawable(R.mipmap.ic_unit_g));
                    break;
                case YELLOW:
                    mViewStatus.setBackground(getContext().getResources().getDrawable(R.mipmap.ic_unit_y));
                    break;
                case ORANGE:
                    mViewStatus.setBackground(getContext().getResources().getDrawable(R.mipmap.ic_unit_o));
                    break;
                case RED:
                    mViewStatus.setBackground(getContext().getResources().getDrawable(R.mipmap.ic_unit_r));
                    break;
                default:
                    mViewStatus.setBackground(getContext().getResources().getDrawable(R.mipmap.ic_unit_g));
                    break;
            }

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

            showLogWhenDev(data);
        }
    }

    private String makeMemberStr(@NonNull DabProjectData data) {
        StringBuilder memberStr = new StringBuilder();

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

    private void showLogWhenDev(@NonNull DabProjectData data) {
        if (HFApplication.DEV_LOG) {
            mTextViewLog.setText(HFLogUtil.getLogData(data));
            mTextViewLog.setVisibility(View.VISIBLE);
        } else {
            mTextViewLog.setVisibility(View.GONE);
        }
    }
}