package com.example.applock.lockadapter;

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.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 java.util.List;
import java.util.Objects;

public class ThirdAdapter extends RecyclerView.Adapter<ThirdAdapter.ThirdVH> {
    private FragmentActivity mycon;
    private List<ATInterstitial> ads;
    private List<AppEntity> mylist;
    private MsgDialog msgDialog;
    private SoftWarePerssionDialog dialog;
    private boolean thirdlock;

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

    @Override
    public void onBindViewHolder(@NonNull ThirdVH 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);
        }
        thirdlock = entityApp.isApplocked();
        if (thirdlock){
            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) {
                            Log.d(SoftWareLockApplication.TAG,"-----mInterstitialAd=null---------------");
                            onAction(entityApp, holder.appBinding.appLocked,true);
                            // TODO: 2024/8/16
                        } else {
                            Mytool.setCallback(mInterstitialAd, new Adcallback() {
                                @Override
                                public void onShowFail(AdError ad) {
                                    Log.d(SoftWareLockApplication.TAG,"-----onShowFail---------------");
                                    onAction(entityApp, holder.appBinding.appLocked,true);
                                }
                                @Override
                                public void onAdHidden() {
                                    Log.d(SoftWareLockApplication.TAG,"-----onAdHidden---------------");
                                    onAction(entityApp, holder.appBinding.appLocked,true);
                                }
                            });
                            mInterstitialAd.show(mycon);
                        }

                    } else {
                        Log.d(SoftWareLockApplication.TAG,"-----不显示广告---------------");
                        onAction(entityApp, holder.appBinding.appLocked,false);
                    }
                    SoftWareLockApplication.userOption++;
                }else {
                    getDialogsuccess();
                }
            }
        });
    }
    private void onAction(AppEntity entity, ImageView imageView, boolean showDailog){
        String msg = "";
        boolean selected = imageView.isSelected();
        if (!selected) {
            entity.setApplocked(false);
            msg = entity.getApplabel() + "unlocked success";
        } else {
            entity.setApplocked(true);
            msg = entity.getApplabel() + "locked success";
        }
        if(showDailog){
            msgDialog = new MsgDialog(msg);
            msgDialog.show(mycon.getSupportFragmentManager(), "");

        }

        SoftWareLockRoom.RUNIO(new Runnable() {
            @Override
            public void run() {
                AppDataBase.getDatabaseInstance().getAppEntityDao().updateData(entity);
            }
        });
    }
    private void getDialogsuccess() {
        boolean isSelect = SoftLockPermission.isGetUsagePermission(mycon);
        boolean isEnable = SoftLockPermission.isOverlays(mycon);
        //Log.d("----eable","eable"+isEnable);
        if (isSelect && isEnable) {
            //goApply();
        } else {
            if (dialog == null) {
                dialog = new SoftWarePerssionDialog(mycon);
            }
            dialog.show(mycon.getSupportFragmentManager(), "");
        }
    }

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

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