package com.navinfo.outdoor.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

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

import com.navinfo.outdoor.R;
import com.navinfo.outdoor.room.PoiDatabase;
import com.navinfo.outdoor.room.PoiEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class StaySubmitAdapter extends RecyclerView.Adapter<StaySubmitAdapter.ViewHolder> {
    private Vector<PoiEntity> allRoad = new Vector<>();
    private Context context;

    public StaySubmitAdapter(Context context) {
        this.context = context;
    }

    public List<PoiEntity> getAllRoad() {
        return allRoad;
    }

    public void setAllRoad(List<PoiEntity> allRoad) {

        this.allRoad.clear();
        this.allRoad.addAll(allRoad);
        notifyDataSetChanged();
    }

    public void setAllDataChecked(boolean isChecked) {
        if (this.allRoad != null) {
            for (PoiEntity entity : this.allRoad) {
                entity.setChecked(isChecked);
            }
        }
    }

    //条目删除
    public void setAllCheckedDelete() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (allRoad) {
                    Iterator iterator = allRoad.iterator();
                    while (iterator.hasNext()) {
                        PoiEntity poiEntity = (PoiEntity) iterator.next();
                        if (poiEntity.isChecked()) {
                            PoiDatabase.getInstance(context).getPoiDao().deletePoiEntity(poiEntity);
                            iterator.remove();
                        }
                    }
                    handler.sendEmptyMessage(0x105);
                }

            }
        }).start();
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.stay_item, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        PoiEntity poiEntity = allRoad.get(position);
        holder.tvName.setText(poiEntity.getName());
        holder.tvDay.setText(poiEntity.getCreateTime());
        //获取checkBox点击的记录
        holder.cbUnSubmit.setChecked(allRoad.get(position).isChecked());
        holder.cbUnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.cbUnSubmit.setChecked(!poiEntity.isChecked());
                poiEntity.setChecked(!poiEntity.isChecked());
            }
        });
        if (poiEntity.getIsLocalData() == 1){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickItem!=null){
                        onClickItem.item(position,poiEntity);
                    }
                }
            });
        }else {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickItem!=null){
                        onClickItem.item(position,poiEntity);
                    }
                }
            });
        }


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private TextView tvDay;
        private CheckBox cbUnSubmit;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_road_name);
            tvDay = itemView.findViewById(R.id.tv_road_day);
            cbUnSubmit = itemView.findViewById(R.id.cb_unSubmit);
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull @NotNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x105:
                    notifyDataSetChanged();
                    break;
            }
        }
    };

   public onClickItem onClickItem ;

    public void setOnClickItem(StaySubmitAdapter.onClickItem onClickItem) {
        this.onClickItem = onClickItem;
    }

    public interface onClickItem {
        void item(int position, PoiEntity allRoad);
    }
}
