package com.example.applock.lockadapter;

import android.content.DialogInterface;
import android.content.SharedPreferences;
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.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.applock.R;
import com.example.applock.allmanager.AppManager;
import com.example.applock.allmanager.MsgDialog;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.databinding.ItemAppBinding;
import com.example.applock.databinding.LockedAppBinding;
import com.example.applock.roombase.AppDataBase;
import com.example.applock.roombase.AppEntity;
import com.example.applock.roombase.SoftWareLockRoom;
import com.example.applock.value.Mytool;
import com.example.applock.value.onActionCallback;

import java.util.ArrayList;
import java.util.List;

public class LockedAdapter extends RecyclerView.Adapter<LockedAdapter.LockedVH> {
    private FragmentActivity mycon;
    private List<AppEntity> mylist = new ArrayList<>();
    private boolean lockedapp = true;

    public LockedAdapter(FragmentActivity context) {

        mycon = context;
    }

    public void setData(List<AppEntity> entities) {
        mylist = entities;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull LockedVH holder, int position) {
        AppEntity entityApp = mylist.get(position);
        String label = entityApp.getApplabel();
        holder.lockedbinding.lockedLabel.setText(label);
        Drawable icon = AppManager.getIcon(mycon, entityApp.getApppackageName());
        if (icon != null) {
            holder.lockedbinding.lockedLogo.setImageDrawable(icon);
        }
        holder.lockedbinding.lockedLocked.setSelected(true);
        holder.lockedbinding.lockedLocked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SoftWareLockApplication.userOption % 5 == 0){
                    Mytool.showAd(mycon, new onActionCallback() {
                        @Override
                        public void onAction() {

                            holder.lockedbinding.lockedLocked.setSelected(!holder.lockedbinding.lockedLocked.isSelected());
                            if (!holder.lockedbinding.lockedLocked.isSelected()) {
                                lockedapp = false;
                                entityApp.setApplocked(false);
                                Toast.makeText(mycon, mycon.getText(R.string.unlock_success), Toast.LENGTH_SHORT).show();
                            } else {
                                lockedapp = true;
                                entityApp.setApplocked(true);
                                Toast.makeText(mycon, mycon.getText(R.string.lock_success), Toast.LENGTH_SHORT).show();
                            }
//                            MsgDialog   msgDialog = new MsgDialog(msg);
//                            msgDialog.show(mycon.getSupportFragmentManager(), "");
                        }
                    });
                }else {
                    holder.lockedbinding.lockedLocked.setSelected(!holder.lockedbinding.lockedLocked.isSelected());
                    if (!holder.lockedbinding.lockedLocked.isSelected()) {
                        lockedapp = false;
                        entityApp.setApplocked(false);
                        Toast.makeText(mycon, mycon.getText(R.string.unlock_success), Toast.LENGTH_SHORT).show();
                    } else {
                        lockedapp = true;
                        entityApp.setApplocked(true);
                        Toast.makeText(mycon, mycon.getText(R.string.lock_success), Toast.LENGTH_SHORT).show();
                    }
                }
                SoftWareLockRoom.RUNIO(new Runnable() {
                    @Override
                    public void run() {
                        AppDataBase.getDatabaseInstance().getAppEntityDao().updateData(entityApp);
                    }
                });

                SoftWareLockApplication.userOption++;
            }
        });
    }

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

    public class LockedVH extends RecyclerView.ViewHolder {
        private LockedAppBinding lockedbinding;

        public LockedVH(@NonNull LockedAppBinding itemView) {
            super(itemView.getRoot());
            lockedbinding = itemView;

        }
    }
}
