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

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
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.TestimonialsActivity;
import com.lc.myapplication.security.locks.databinding.TuijianAppsViewBinding;
import com.lc.myapplication.security.locks.dialog.MsgDialog;
import com.lc.myapplication.security.locks.dialog.PermissionDialog;
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.LockPremission;
import com.lc.myapplication.security.locks.tool.MyTools;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TuijianAdapter extends RecyclerView.Adapter<TuijianAdapter.TuijianAppVH>{
    private TuijianAppsViewBinding tuijianAppsViewBinding;
    private List<SecurityLockEntity> securityLockEntities;
    private TestimonialsActivity mycon;
    private MsgDialog msgDialog = null;
    private PermissionDialog dialog;
    public TuijianAdapter(TestimonialsActivity context, List<SecurityLockEntity> list){
        mycon = context;
        securityLockEntities = list;
    }

    @NonNull
    @Override
    public TuijianAppVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        tuijianAppsViewBinding = TuijianAppsViewBinding.inflate(LayoutInflater.from(mycon),null,false);
        return new TuijianAppVH(tuijianAppsViewBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull TuijianAppVH holder, int position) {
                SecurityLockEntity entity = securityLockEntities.get(position);
                holder.tuijianAppsViewBinding.tuijianReName.setText(entity.getApplsign());
                Drawable drawable = AppsManager.getapplogo(mycon,entity.getApppakname());
                holder.tuijianAppsViewBinding.tuijianReLogo.setImageDrawable(drawable);
                boolean locksuccess = MyTools.getTest(entity.getApppakname());
                holder.tuijianAppsViewBinding.tuijianReSwitch.setOnCheckedChangeListener(null);
                if (locksuccess) {
                    holder.tuijianAppsViewBinding.tuijianReSwitch.setChecked(true);
                } else {
                    holder.tuijianAppsViewBinding.tuijianReSwitch.setChecked(false);
                }
                holder.tuijianAppsViewBinding.tuijianReSwitch.setChecked(entity.isLocksuccess());
                holder.tuijianAppsViewBinding.tuijianReSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (LockPremission.privacysuccess(mycon) && LockPremission.showonsuccess(mycon)){
                            if (Securitylocks.userOption % 5 == 0){
                                showMax(new ActionListener() {
                                    @Override
                                    public void onAction() {
                                        String msg = "";
                                        if (isChecked) {
                                            msg = entity.getApplsign() + "locked success";
                                        } else {
                                            msg = entity.getApplsign() + "unlocked success";
                                        }
                                        msgDialog = new MsgDialog(msg);
                                        msgDialog.show(mycon.getSupportFragmentManager(), "");
                                    }
                                });
                            }
                            Securitylocks.userOption++;
                            MyTools.settest(entity.getApppakname(), locksuccess);
                            entity.setLocksuccess(isChecked);
                            MyTools.RUNIO(new Runnable() {
                                @Override
                                public void run() {
                                    SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().Updataentity(entity);
                                }
                            });
                        }else {
                            holder.tuijianAppsViewBinding.tuijianReSwitch.setChecked(false);
                            getDialogsuccess();
                        }

                    }
                });
    }
    private void getDialogsuccess() {
        boolean isSelect = LockPremission.privacysuccess(mycon);
        boolean isEnable = LockPremission.showonsuccess(mycon);
        Log.d("----eable","eable"+isEnable);
        if (isSelect && isEnable) {
            //goApply();
        } else {
            if (dialog == null) {
                dialog = new PermissionDialog(mycon);
            }
            dialog.show(mycon.getSupportFragmentManager(), "");
        }
    }





    @Override
    public int getItemCount() {
        return securityLockEntities.size()/2;
    }

    public class TuijianAppVH extends RecyclerView.ViewHolder {
        TuijianAppsViewBinding tuijianAppsViewBinding;
        public TuijianAppVH(@NonNull TuijianAppsViewBinding itemView) {
            super(itemView.getRoot());
            tuijianAppsViewBinding = itemView;
        }
    }

    private void showMax(ActionListener listener) {
        MaxManager.ShowAd(mycon, 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();
                }
            }
        });
    }
}
