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

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

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.data.model.TaskHistoryData;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.view.HFDotTextView;

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

public class TaskHistoryViewHolder extends BaseViewHolder {
    private TextView mTextViewRegTime;
    private LinearLayout mLayoutDetail;
    private View mViewLine;

    public TaskHistoryViewHolder(@NonNull ViewGroup viewGroup) {

        super(viewGroup, R.layout.item_task_history_list);

        mTextViewRegTime = (TextView) itemView.findViewById(R.id.regTime_textView);
        mLayoutDetail = (LinearLayout) itemView.findViewById(R.id.detail_linearLayout);
        mViewLine = (View) itemView.findViewById(R.id.line_view);
    }

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

        final TaskHistoryData data = (TaskHistoryData) object;
        if (data != null) {
            mLayoutDetail.removeAllViews();

            ArrayList<String> logList = data.getListLogDetail();
            for (int i = 0; i < logList.size(); i++) {
                HFDotTextView view = new HFDotTextView(getContext());
                view.setText(logList.get(i));
                view.setVisibleDot(logList.size() > 1);
                mLayoutDetail.addView(view);
            }

            String displayTime = HFDateUtil.getChangeDateFormat(data.getLogged_at(), HFDateUtil.FORMAT_COMMON_SERVER, HFDateUtil.FORMAT_COMMON_SERVER);
            mTextViewRegTime.setText(displayTime);

            int dp1 = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp1);

            itemView.measure(0, 0);
            int measuredHeight = itemView.getMeasuredHeight();
            if (index == 0) {
                int height = measuredHeight - HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp10);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dp1, height);
                params.topMargin = HFCommonUtil.getPixelFromDimension(getContext(), R.dimen.dimen_dp10);
                params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                mViewLine.setLayoutParams(params);
            } else {
                int height = measuredHeight;
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dp1, height);
                params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                mViewLine.setLayoutParams(params);
            }
        }
    }
}
