package com.hw.nativeapp.ui.activity.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hw.nativeapp.R;
import com.hw.nativeapp.httpnet.entity.handleBean;
import com.hw.nativeapp.ui.activity.holders.AftermarketOneHolder;
import com.hw.nativeapp.ui.activity.holders.TicketCollectionHolder;
import com.hw.nativeapp.utils.ImageUtils;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

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


    private Context mContext;
    private List<handleBean> mDatas ;
    private OnBtnClickListerer mOnReplenishBtnClickListerer;
    private OnBtnClickListerer mOnOutBtnClickListerer;
    private Instant Glide;


    public TicketCollectionAdapter(Context context) {
        mContext = context;
        mDatas = new ArrayList<>();
    }

    public void addDatas(List<handleBean> datas){
        if (mDatas == null){
            mDatas = new ArrayList<>();
        }
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clearDatas(){
        if (mDatas != null){
            mDatas.clear();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_ticket_collection, parent, false);
        return new TicketCollectionHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TicketCollectionHolder normalHolder = (TicketCollectionHolder) holder;
        handleBean detailBean = mDatas.get(position);
        if (detailBean == null){
            return;
        }

        normalHolder.iv_performName.setText(detailBean.performName);
        normalHolder.iv_performDate.setText(detailBean.performDate + " " + detailBean.performStart.substring(11, 16) + "-" + detailBean.performEnd.substring(11, 16));
        normalHolder.cb_check.setChecked(detailBean.checkedStatus);
//        normalHolder.iv_performStart_performEnd.setText("场次 " + detailBean.performStart.substring(11, 16) + "-" + detailBean.performEnd.substring(11, 16));
        normalHolder.iv_seat.setText(detailBean.seatName + "(" +  detailBean.seatTypeName  +")");
        Picasso.with(mContext).load(detailBean.showImg).into(normalHolder.iv_showImg);
        normalHolder.iv_name.setText(detailBean.name + " " +  detailBean.idcard);
        normalHolder.cb_check.setOnClickListener(v -> {
            detailBean.checkedStatus =  normalHolder.cb_check.isChecked();
            mOnOutBtnClickListerer.onClick(detailBean, position);
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void setOnOutBtnClickListerer(OnBtnClickListerer onBtnClickListerer) {
        this.mOnOutBtnClickListerer = onBtnClickListerer;
    }

    public interface OnBtnClickListerer{
        void onClick(handleBean bean, int position);
    }
}
