package com.sunzee.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunzee.R;
import com.sunzee.model.domain.WarringBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author whw
 * @time 2019/3/21
 * @Description 报警记录适配器
 */
public class RvAlarmAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<WarringBean> datas;

    public void setItemListener(ItemMaintainClickListener listener) {
        this.listener = listener;
    }

    private ItemMaintainClickListener listener;

    public RvAlarmAdapter(List<WarringBean> datas) {
        this.datas = datas;
    }

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_NORMAL = 1;


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View inflate;
        if(viewType==TYPE_HEADER){
            inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_alarm, viewGroup, false);
            return  new RvAlarmAdapter.ViewHolderHeader(inflate);
        }
        inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_alarm, viewGroup, false);
        return new RvAlarmAdapter.ViewHolder(inflate);
    }



    @Override
    public int getItemViewType(int position) {
        //头布局
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        if (viewHolder instanceof ViewHolderHeader){
            ViewHolderHeader viewHolderHeader= (ViewHolderHeader) viewHolder;
            viewHolderHeader.tvTime.setTextColor(R.color.logo_blue);
            viewHolderHeader.tvThingError.setTextColor(R.color.logo_blue);
            viewHolderHeader.tvRemark.setTextColor(R.color.logo_blue);
        }else {
            ViewHolder viewHolderNormal= (ViewHolder) viewHolder;
            viewHolderNormal.tvTime.setTextColor(R.color.colorGray);
            viewHolderNormal.tvThingError.setTextColor(R.color.colorGray);
            viewHolderNormal.tvRemark.setTextColor(R.color.colorGray);
            WarringBean warringBean = datas.get(i - 1);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            Date curDate = new Date(warringBean.getTime());
            String time = formatter.format(curDate);
            viewHolderNormal.tvTime.setText(time);
            viewHolderNormal.tvThingError.setText(warringBean.getThingError());
            viewHolderNormal.tvRemark.setText(warringBean.getRemark());
        }
    }

    @Override
    public int getItemCount() {
        return datas.size()+1;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTime;
        TextView tvThingError;
        TextView tvRemark;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTime=itemView.findViewById(R.id.tv_time);
            tvThingError=itemView.findViewById(R.id.tv_error_thing);
            tvRemark = itemView.findViewById(R.id.tv_remark);
        }
    }


    public static class ViewHolderHeader extends RecyclerView.ViewHolder {
        TextView tvTime;
        TextView tvThingError;
        TextView tvRemark;

        public ViewHolderHeader(@NonNull View itemView) {
            super(itemView);
            tvTime=itemView.findViewById(R.id.tv_time);
            tvThingError=itemView.findViewById(R.id.tv_error_thing);
            tvRemark = itemView.findViewById(R.id.tv_remark);
        }
    }
    public interface ItemMaintainClickListener {
        /**
         * @param view 控件
         * @param position 位置
         *
         */
        void onItemListener(View view, int position);

    }
}
