package com.example.applock.lockadapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.example.applock.R;
import com.example.applock.allmanager.AppManager;
import com.example.applock.allmanager.MsgDialog;
import com.example.applock.allmanager.MyWorker;
import com.example.applock.allmanager.SoftLockPermission;
import com.example.applock.allmanager.SoftWarePerssionDialog;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.databinding.ItemAppBinding;
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 com.example.applock.value.Adcallback;
import com.example.applock.value.MyAllValues;
import com.example.applock.value.Mytool;
import com.example.applock.value.onShowDialogCallback;
import com.pgl.ssdk.S;

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

public class SystemAdapter extends RecyclerView.Adapter<SystemAdapter.SystemVH> {
    private Activity mycon;
    public static SharedPreferences.Editor keyeditor;
    private List<ATInterstitial> ads;
    private List<AppEntity> mylist = new ArrayList<>();
    private LockListener lockListener;
    private SoftWarePerssionDialog dialog;
    private boolean mLockList;
    private MsgDialog msgDialog;

    private onShowDialogCallback onShowDialogCallback;

    public void setOnShowDialogCallback(onShowDialogCallback callback) {
        this.onShowDialogCallback = callback;
    }

    public SystemAdapter(Activity context){
        mycon = context;
        ads = SoftWareLockApplication.lodAd();
    }
    public void setData(List<AppEntity> entities) {
        mylist = entities;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public SystemVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemAppBinding itemAppBinding = ItemAppBinding.inflate(LayoutInflater.from(mycon),parent,false);
        return new SystemVH(itemAppBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull SystemVH holder, int position) {
        AppEntity entityApp = mylist.get(position);
        String label = entityApp.getApplabel();
        holder.appBinding.appLabel.setText(label);
        Drawable icon = AppManager.getIcon(mycon, entityApp.getApppackageName());
        if (icon != null) {
            holder.appBinding.appLogo.setImageDrawable(icon);
        }

        mLockList = entityApp.isApplocked();
        if (mLockList){
            holder.appBinding.appLocked.setSelected(true);
        }else {
            holder.appBinding.appLocked.setSelected(false);
        }
        holder.appBinding.appLocked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SoftLockPermission.allAllowPermission(mycon)){
                    holder.appBinding.appLocked.setSelected(!holder.appBinding.appLocked.isSelected());
                    if ( SoftWareLockApplication.userOption % 5 == 0) {
                        ATInterstitial mInterstitialAd = Mytool.onCache(ads);
                        if (mInterstitialAd == null) {
                            onAction(entityApp, holder.appBinding.appLocked,true);
                            // TODO: 2024/8/16
                        } else {
                            Mytool.setCallback(mInterstitialAd, new Adcallback() {
                                @Override
                                public void onShowFail(AdError ad) {

                                    onAction(entityApp, holder.appBinding.appLocked,true);
                                }
                                @Override
                                public void onAdHidden() {
                                    onAction(entityApp, holder.appBinding.appLocked,true);
                                }
                            });
                            mInterstitialAd.show(mycon);
                        }

                    } else {
                        onAction(entityApp, holder.appBinding.appLocked,false);
                    }
                    SoftWareLockApplication.userOption++;
                }else {
                    getDialogsuccess();
                }
            }
        });
    }

    private void onAction(AppEntity tmpEntity,ImageView imageView,boolean showDailog){

        String msg = "";
        boolean selected = imageView.isSelected();
        if (!selected) {
            tmpEntity.setApplocked(false);
            msg = tmpEntity.getApplabel() + " unlocked success";
        } else {
            tmpEntity.setApplocked(true);
            msg = tmpEntity.getApplabel() + " locked success";
        }
        SoftWareLockRoom.RUNIO(new Runnable() {
            @Override
            public void run() {
                AppDataBase.getDatabaseInstance().getAppEntityDao().updateData(tmpEntity);
                Log.d(SoftWareLockApplication.TAG,"-----updateData---------------");
            }
        });
        if(showDailog){
            if(onShowDialogCallback!= null){
                onShowDialogCallback.onShowDialog(msg);
            }
        }



    }
    private void getDialogsuccess() {
        boolean isSelect = SoftLockPermission.isGetUsagePermission(mycon);
        boolean isEnable = SoftLockPermission.isOverlays(mycon);
        if (isSelect && isEnable) {
            //goApply();
        } else {
            if(onShowDialogCallback!= null){
                onShowDialogCallback.onShowStepDialog();
            }
        }
    }
    @Override
    public int getItemCount() {
        return mylist.size();
    }

    public class SystemVH extends RecyclerView.ViewHolder {
        private ItemAppBinding appBinding;
        public SystemVH(@NonNull ItemAppBinding itemView) {
            super(itemView.getRoot());
            appBinding = itemView;
        }
    }
}
