package com.nscg.app.view.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nscg.app.R;
import com.nscg.app.model.domain.GroupInfo;
import com.nscg.app.view.activity.TaskListActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 任务列表适配器
 * Created by liuyongfeng on 2017/5/9.
 */

public class TaskGroupAdapter extends RecyclerView.Adapter<TaskGroupAdapter.ViewHolder> {


    private ArrayList<GroupInfo> groupInfos = new ArrayList<>();
    private Activity mActivity;

    public TaskGroupAdapter(Activity activity, ArrayList<GroupInfo> groupInfos) {
        this.groupInfos = groupInfos;
        this.mActivity = activity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.group_recycleview_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final GroupInfo groupInfo = groupInfos.get(position);
        if (groupInfo.getLevle() == 2) {
            /*属于紧急任务*/
            holder.txtTitle.setTextColor(Color.RED);
            holder.txtCount.setTextColor(Color.RED);
        } else if (groupInfo.getLevle() == 1) {
            /*回退待确认*/
            holder.txtTitle.setTextColor(ContextCompat.getColor(mActivity, R.color.holo_orange_dark));
            holder.txtCount.setTextColor(ContextCompat.getColor(mActivity, R.color.holo_orange_dark));
        } else {
            holder.txtTitle.setTextColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));
            holder.txtCount.setTextColor(ContextCompat.getColor(mActivity, R.color.colorPrimary));
        }
        holder.txtTitle.setText(groupInfo.getName());
        holder.txtCount.setText(Html.fromHtml(groupInfo.getTotalCount() > 0
                ? "(<font color='#FF0000'>" + groupInfo.getUploadCount() + "</font>/"
                + groupInfo.getTotalCount() + ")"
                : "(<font color='#FF0000'>0</font>/0)"));

        holder.GroupView.setOnClickListener(view -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("group", groupInfo);
            Intent intent = new Intent(mActivity, TaskListActivity.class);
            intent.putExtras(bundle);
            mActivity.startActivity(intent);
        });

    }

    @Override
    public int getItemCount() {
        return groupInfos == null ? 0 : groupInfos.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        View GroupView;
        @BindView(R.id.txtTitle)
        TextView txtTitle;
        @BindView(R.id.txtCount)
        TextView txtCount;

        public ViewHolder(View itemView) {
            super(itemView);
            GroupView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
}
