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

import static com.unity3d.services.core.properties.ClientProperties.getApplicationContext;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Toast;

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

import com.bytedance.frameworks.encryptor.BuildConfig;
import com.lc.myapplication.security.locks.R;
import com.lc.myapplication.security.locks.Securitylocks;
import com.lc.myapplication.security.locks.activity.AllAppActivity;
import com.lc.myapplication.security.locks.databinding.AllAppsViewBinding;
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 com.lc.myapplication.security.locks.value.MeValues;

import java.util.List;
import java.util.Objects;

public class AllAppsAdapter extends RecyclerView.Adapter<AllAppsAdapter.AllAppVH> {
    private AllAppActivity mActivity;
    private List<SecurityLockEntity> securityLockEntities;

    private MsgDialog msgDialog = null;
    private PermissionDialog dialog;

    private onAdStatusListener listener;
    String packageName = getApplicationContext().getPackageName();

    public void setListener(onAdStatusListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public String toString() {
        return super.toString();
    }

    public AllAppsAdapter(AllAppActivity context, List<SecurityLockEntity> entities) {
        mActivity = context;
        this.securityLockEntities = entities;
    }


    @NonNull
    @Override
    public AllAppVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AllAppsViewBinding allAppsViewBinding = AllAppsViewBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new AllAppVH(allAppsViewBinding);
    }


    @Override
    public void onBindViewHolder(@NonNull AllAppVH holder, int position) {
        SecurityLockEntity entity = securityLockEntities.get(position);
        String appsSign = entity.getApplsign();
        holder.allAppsViewBinding.appReName.setText(appsSign);
        Drawable drawable = AppsManager.getapplogo(MeValues.lock, entity.getApppakname());
        holder.allAppsViewBinding.appReLogo.setImageDrawable(drawable);

        boolean locksuccess = MyTools.getTest(entity.getApppakname());
        holder.allAppsViewBinding.appReSwitch.setOnCheckedChangeListener(null);
        if (locksuccess) {
            holder.allAppsViewBinding.appReSwitch.setChecked(true);
        } else {
            holder.allAppsViewBinding.appReSwitch.setChecked(false);
        }
        holder.allAppsViewBinding.appReSwitch.setChecked(entity.isLocksuccess());
        holder.allAppsViewBinding.appReSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (LockPremission.privacysuccess(mActivity) && LockPremission.showonsuccess(mActivity)) {
                    MyTools.settest(entity.getApppakname(), locksuccess);
                    if (Securitylocks.userOption % 5 == 0) {
                            showMax(new ActionListener() {
                                @Override
                                public void onAction() {

                                    MyTools.settest(entity.getApppakname(), locksuccess);

                                    entity.setLocksuccess(isChecked);
                                    MyTools.RUNIO(new Runnable() {
                                        @Override
                                        public void run() {
                                            SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().Updataentity(entity);
                                            // Log.d("----sign2", "----------" + entity.getApppakname() + "--------" + entity.isLocksuccess());
                                        }
                                    });
                                    String msg = "";
                                    if (isChecked) {
                                        msg = entity.getApplsign() + "locked success";
                                    } else {
                                        msg = entity.getApplsign() + "unlocked success";
                                    }
                                    msgDialog = new MsgDialog(msg);
                                    msgDialog.show(mActivity.getSupportFragmentManager(), "");
                                }
                            });
                    } else {
                            MyTools.settest(entity.getApppakname(), locksuccess);
                            entity.setLocksuccess(isChecked);
                            MyTools.RUNIO(new Runnable() {
                                @Override
                                public void run() {
                                    SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().Updataentity(entity);
                                    // Log.d("----sign2", "----------" + entity.getApppakname() + "--------" + entity.isLocksuccess());
                                }
                            });
                    }
                    Securitylocks.userOption++;
                }else{
                        holder.allAppsViewBinding.appReSwitch.setChecked(false);
                        getDialogsuccess();
                    }
               // Log.d(Securitylocks.TAG, "----------" + entity.getApppakname() + "-----" + isChecked);

            }
        });

    }

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

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

    public class AllAppVH extends RecyclerView.ViewHolder {
        private AllAppsViewBinding allAppsViewBinding;

        public AllAppVH(@NonNull AllAppsViewBinding itemView) {
            super(itemView.getRoot());
            allAppsViewBinding = itemView;
        }
    }


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