package com.example.applock.lockadapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.applock.R;
import com.example.applock.allmanager.AppManager;
import com.example.applock.databinding.RecycleSearchViewBinding;
import com.example.applock.listener.LockListener;
import com.example.applock.roombase.AppDataBase;
import com.example.applock.roombase.AppEntity;
import com.example.applock.roombase.SoftWareLockRoom;

import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchVH> {
    private RecycleSearchViewBinding recycleSearchViewBinding;
    private Context mycon;
    private List<AppEntity> entityList;
    private boolean searchlock;
    public SearchAdapter(Context context, List<AppEntity> appEntities){
        mycon = context;
        entityList = appEntities;
    }
    @NonNull
    @Override
    public SearchVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        recycleSearchViewBinding = RecycleSearchViewBinding.inflate(LayoutInflater.from(mycon),parent,false);
        return new SearchVH(recycleSearchViewBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchVH holder, @SuppressLint("RecyclerView") int position) {
        AppEntity entityApp = entityList.get(position);
        String label = entityApp.getApplabel();
        holder.searchViewBinding.rearchAppLabel.setText(label);
        Drawable icon = AppManager.getIcon(mycon, entityApp.getApppackageName());
        if (icon != null) {
            holder.searchViewBinding.rearchAppLogo.setImageDrawable(icon);
        }
        searchlock = entityApp.isApplocked();
        if (searchlock){
            holder.searchViewBinding.rearchAppLock.setSelected(true);
        }else {
            holder.searchViewBinding.rearchAppLock.setSelected(false);
        }
        holder.searchViewBinding.rearchAppLock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.searchViewBinding.rearchAppLock.setSelected(! holder.searchViewBinding.rearchAppLock.isSelected());
                boolean select = holder.searchViewBinding.rearchAppLock.isSelected();
                if (!select) {
                    entityApp.setApplocked(false);
                    Toast.makeText(mycon,"unlocked success",Toast.LENGTH_SHORT).show();
                } else {
                    entityApp.setApplocked(true);
                    Toast.makeText(mycon,"locked success",Toast.LENGTH_SHORT).show();
                }
                SoftWareLockRoom.RUNIO(new Runnable() {
                    @Override
                    public void run() {
                        AppDataBase.getDatabaseInstance().getAppEntityDao().updateData(entityApp);
                    }
                });
            }
        });
    }

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

    public class SearchVH extends RecyclerView.ViewHolder {
        private RecycleSearchViewBinding searchViewBinding;
        public SearchVH(RecycleSearchViewBinding recycleSearchViewBinding) {
            super(recycleSearchViewBinding.getRoot());
            searchViewBinding = recycleSearchViewBinding;
        }
    }
}
