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

import android.support.annotation.NonNull;
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.MainProjectListContract;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.ProjectTaskInfo;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.util.HFTaskUtil;

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

public class MainProjectTaskListViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutContainer;
    private ImageView mImageViewStatus;
    private TextView mTextViewTitle;
    private TextView mTextViewDueDate;
    private TextView mTextViewReceiverName;
    private View mViewBottomMargin;
    private View mViewTopPadding;
    private View mViewBottomPadding;
    private TextView mTextViewLog;

    private int mTotalCount = 0;

    private MainProjectListContract.OnMainProjectListListener mOnMainProjectListListener;

    public MainProjectTaskListViewHolder(@NonNull ViewGroup viewGroup, MainProjectListContract.OnMainProjectListListener onMainProjectListListener) {
        super(viewGroup, R.layout.item_main_project_task_list);

        mOnMainProjectListListener = onMainProjectListListener;

        mLayoutContainer = (LinearLayout) itemView.findViewById(R.id.container_linearLayout);
        mImageViewStatus = (ImageView) itemView.findViewById(R.id.status_imageView);
        mTextViewTitle = (TextView) itemView.findViewById(R.id.title_textView);
        mTextViewDueDate = (TextView) itemView.findViewById(R.id.dueDate_textView);
        mTextViewReceiverName = (TextView) itemView.findViewById(R.id.receiverName_textView);
        mViewBottomMargin = (View) itemView.findViewById(R.id.bottomMargin_view);
        mViewTopPadding = (View) itemView.findViewById(R.id.topPadding_view);
        mViewBottomPadding = (View) itemView.findViewById(R.id.bottomPadding_view);
        mTextViewLog = (TextView) itemView.findViewById(R.id.log_textView);
    }

    public void setTotalCount(int totalCount) {
        mTotalCount = totalCount;
    }

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

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

            if (index == 0) {
                mViewTopPadding.setVisibility(View.VISIBLE);
            } else {
                mViewTopPadding.setVisibility(View.GONE);
            }

            if (mTotalCount - index == 1) {
                mViewBottomPadding.setVisibility(View.VISIBLE);
                mViewBottomMargin.setVisibility(View.VISIBLE);
                if (data.isOpen())
                    mLayoutContainer.setBackground(getContext().getResources().getDrawable(R.drawable.box_rounded_project_task_bg_bottom));
                else
                    mLayoutContainer.setBackground(getContext().getResources().getDrawable(R.drawable.box_rounded_project_task_closed_bg_bottom));
            } else {
                mViewBottomPadding.setVisibility(View.GONE);
                mViewBottomMargin.setVisibility(View.GONE);
                if (data.isOpen())
                    mLayoutContainer.setBackground(getContext().getResources().getDrawable(R.drawable.box_rounded_project_task_bg_middle));
                else
                    mLayoutContainer.setBackground(getContext().getResources().getDrawable(R.drawable.box_rounded_project_task_closed_bg_middle));
            }

            mImageViewStatus.setImageDrawable(data.isToday() ? getContext().getResources().getDrawable(R.drawable.circle_blue02) :
                    getContext().getResources().getDrawable(R.drawable.circle_grey28));

            mTextViewTitle.setText(data.getDesc());

            mTextViewReceiverName.setText(data.getMember_name());

            if (data.isOpen()) {
                mTextViewTitle.setTextColor(HFCommonUtil.getColor(getContext(), R.color.hf_grey01));

                mTextViewDueDate.setTextColor(HFTaskUtil.getDueDateFontColor(getContext(), data.getDue_to(), null));
                mTextViewDueDate.setText(HFTaskUtil.getDueDateText(getContext(), data.getDue_to(), null));

                mImageViewStatus.setVisibility(View.VISIBLE);
            } else {
                mTextViewTitle.setTextColor(HFCommonUtil.getColor(getContext(), R.color.hf_grey03));

                mTextViewDueDate.setTextColor(HFCommonUtil.getColor(getContext(), R.color.hf_grey03));
                mTextViewDueDate.setText(HFDateUtil.getChangeDateFormat(data.getClosed_at(), HFDateUtil.FORMAT_COMMON_SERVER, HFDateUtil.FORMAT_YYYY_MM_DD));

                mImageViewStatus.setVisibility(View.INVISIBLE);
            }

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

            // Log
            if (HFApplication.DEV_LOG) {
                String log = "TASK : " + data.getId() +
                        " MemberId : " + data.getMember_id();

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