package com.sheep.gamegroup.view.adapter.expandadapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sheep.gamegroup.model.entity.PunchLogEntity;
import com.sheep.jiuyan.samllsheep.R;

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



public class SignRecordAdapter extends BaseRecyclerViewAdapter<String, PunchLogEntity,SignRecordViewHolder> {

    private Context ctx;
    private LayoutInflater mInflater;
    SimpleDateFormat df ;

    public SignRecordAdapter(Context ctx, List<RecyclerViewData> datas) {
        super(ctx, datas);
        mInflater = LayoutInflater.from(ctx);
        this.ctx = ctx;
        df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
    }

    /**
     * head View数据设置
     * @param holder
     * @param groupPos
     * @param position
     * @param groupData
     */
    @Override
    public void onBindGroupHolder(SignRecordViewHolder holder, int groupPos, int position, String groupData,boolean isExpand) {
        if(isExpand){
            holder.sign_group_iv_course.setImageResource(R.drawable.sign_up_bg);
        }else{
            holder.sign_group_iv_course.setImageResource(R.drawable.sign_right_bg);
        }
        holder.sign_group_tv_title.setText(groupData);
    }

    /**
     * child View数据设置
     * @param holder
     * @param groupPos
     * @param childPos
     * @param position
     * @param childData
     */
    @Override
    public void onBindChildpHolder(SignRecordViewHolder holder, int groupPos, int childPos, int position, PunchLogEntity childData) {
        holder.sign_child_tv_text.setText(getSpannableString(childData.getAmount()));
        holder.sign_child_tv_time.setText(df.format(new Date(childData.getCreateTime() * 1000)));
    }

    @Override
    public View getGroupView(ViewGroup parent) {
        return mInflater.inflate(R.layout.sign_record_item_group,parent,false);
    }

    @Override
    public View getChildView(ViewGroup parent) {
        return mInflater.inflate(R.layout.sign_record_item_child,parent,false);
    }

    @Override
    public SignRecordViewHolder createRealViewHolder(Context ctx, View view, int viewType) {
        return new SignRecordViewHolder(ctx,view,viewType);
    }

    /**
     * true 全部可展开
     * fasle  同一时间只能展开一个
     * */
    @Override
    public boolean canExpandAll() {
        return false;
    }

    private SpannableString getSpannableString(String price) {
        String text = String.format(ctx.getResources().getString(R.string.sign_record_item_money),price);
        SpannableString spannableString = new SpannableString(text);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff0006"));
        spannableString.setSpan(colorSpan, text.length() - 1 - price.length(), text.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

}
