package com.lc.myapplication.security.locks.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CompoundButton;

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

import com.lc.myapplication.security.locks.databinding.RearchRecycleViewBinding;
import com.lc.myapplication.security.locks.lockroom.SecurityLockDataBase;
import com.lc.myapplication.security.locks.lockroom.SecurityLockEntity;
import com.lc.myapplication.security.locks.tool.AppsManager;
import com.lc.myapplication.security.locks.tool.MyTools;

import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchVH>{
    private Context mycon;
    private List<SecurityLockEntity> entity;
    public SearchAdapter(Context context, List<SecurityLockEntity> entitylist){
        mycon = context;
        entity = entitylist;
    }

    @NonNull
    @Override
    public SearchVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RearchRecycleViewBinding rearchRecycleViewBinding = RearchRecycleViewBinding.inflate(LayoutInflater.from(mycon),parent,false);
        return new SearchVH(rearchRecycleViewBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchVH holder, int position) {
        SecurityLockEntity securityLockEntity = entity.get(position);
        holder.rearchBinding.rearchName.setText(securityLockEntity.getApplsign());
        Drawable drawable = AppsManager.getapplogo(mycon,securityLockEntity.getApppakname());
        holder.rearchBinding.rearchImage.setImageDrawable(drawable);
        holder.rearchBinding.rearchReSwitch.setOnCheckedChangeListener(null);
        holder.rearchBinding.rearchReSwitch.setChecked(securityLockEntity.isLocksuccess());
        holder.rearchBinding.rearchReSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                securityLockEntity.setLocksuccess(isChecked);
                MyTools.RUNIO(new Runnable() {
                    @Override
                    public void run() {
                        SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().Updataentity(securityLockEntity);
                    }
                });
            }
        });
    }

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

    public class SearchVH extends RecyclerView.ViewHolder {
        private RearchRecycleViewBinding rearchBinding;
        public SearchVH(@NonNull RearchRecycleViewBinding itemView) {
            super(itemView.getRoot());
            rearchBinding = itemView;
        }
    }
}
