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

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

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

import com.lc.myapplication.security.locks.Securitylocks;
import com.lc.myapplication.security.locks.activity.LockedAppActivity;
import com.lc.myapplication.security.locks.databinding.LockedAppsViewBinding;
import com.lc.myapplication.security.locks.dialog.MsgDialog;
import com.lc.myapplication.security.locks.listener.ActionListener;
import com.lc.myapplication.security.locks.lockroom.SecurityLockDataBase;
import com.lc.myapplication.security.locks.lockroom.SecurityLockEntity;
import com.lc.myapplication.security.locks.max.MaxManager;
import com.lc.myapplication.security.locks.max.onAdStatusListener;
import com.lc.myapplication.security.locks.tool.AppsManager;
import com.lc.myapplication.security.locks.tool.MyTools;

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

public class LockedAppAdapter extends RecyclerView.Adapter<LockedAppAdapter.LockedAppVH> {
    private LockedAppActivity mcon;
    private List<SecurityLockEntity> entityList;
    private MsgDialog msgDialog = null;
    public LockedAppAdapter(LockedAppActivity context, List<SecurityLockEntity> entities){
        mcon = context;
        entityList = entities;
    }
    @NonNull
    @Override
    public LockedAppVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LockedAppsViewBinding lockedAppsViewBinding = LockedAppsViewBinding.inflate(LayoutInflater.from(mcon),null,false);
        return new LockedAppVH(lockedAppsViewBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull LockedAppVH holder, int position) {
        SecurityLockEntity securityLockEntity = entityList.get(position);
        holder.lockedAppsViewBinding.lockedReName.setText(securityLockEntity.getApplsign());
        Drawable drawable = AppsManager.getapplogo(mcon,securityLockEntity.getApppakname());
        holder.lockedAppsViewBinding.lockedReLogo.setImageDrawable(drawable);
        holder.lockedAppsViewBinding.lockedReSwitch.setOnCheckedChangeListener(null);
        holder.lockedAppsViewBinding.lockedReSwitch.setChecked(true);
        holder.lockedAppsViewBinding.lockedReSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (Securitylocks.userOption % 5 == 0){
                    showMax(new ActionListener() {
                        @Override
                        public void onAction() {
                            String msg = "";
                            if (isChecked) {
                                msg = securityLockEntity.getApplsign() + "locked success";
                            } else {
                                msg = securityLockEntity.getApplsign() + "unlocked success";
                            }
                            msgDialog = new MsgDialog(msg);
                            msgDialog.show(mcon.getSupportFragmentManager(), "");

                        }
                    });
                }
                Securitylocks.userOption++;
                securityLockEntity.setLocksuccess(isChecked);
                MyTools.RUNIO(new Runnable() {
                    @Override
                    public void run() {
                        SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().Updataentity(securityLockEntity);
                    }
                });
            }
        });
    }

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

    public class LockedAppVH extends RecyclerView.ViewHolder {
        private LockedAppsViewBinding lockedAppsViewBinding;
        public LockedAppVH(@NonNull LockedAppsViewBinding itemView) {
            super(itemView.getRoot());
            lockedAppsViewBinding = itemView;
        }
    }
    private void showMax(ActionListener listener) {
        MaxManager.ShowAd(mcon, new onAdStatusListener() {
            @Override
            public void onAdStatus(int type) {
                if (type == MaxManager.type_show_close || type == MaxManager.type_show_fail || type == MaxManager.type_no_cache) {
                    if (listener != null)
                        listener.onAction();
                }
            }
        });
    }

}
