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.Toast;

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

import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.example.applock.activity.KeyWordActivity;
import com.example.applock.allmanager.AppManager;
import com.example.applock.allmanager.Datamanager;
import com.example.applock.application.SoftWareLockApplication;
import com.example.applock.databinding.ItemAppBinding;
import com.example.applock.listener.InputKeyListener;
import com.example.applock.listener.LockListener;
import com.example.applock.listener.SwitchListener;
import com.example.applock.roombase.AppEntity;
import com.example.applock.roombase.SoftWareLockRoom;

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

import com.example.applock.R;
import com.example.applock.value.Adcallback;
import com.example.applock.value.Mytool;

public class SoftWareAdapter extends RecyclerView.Adapter<SoftWareAdapter.SoftWareViewHolder> {
    private List<AppEntity> entityApp1 = new ArrayList<>();

    private Activity mCon;

    private boolean mLockList;
    private boolean lock;
    private LockListener lockListener;
    private SwitchListener switchListener;
    public static SharedPreferences keysp;
    public static SharedPreferences.Editor keyeditor;
    private int keyid;
    private List<ATInterstitial> ads;

    public SoftWareAdapter(Activity context, boolean lock) {
        this.mCon = context;
        this.mLockList = lock;
        keysp = mCon.getSharedPreferences("key", Context.MODE_PRIVATE);
        ads = SoftWareLockApplication.lodAd();
    }

    public void setLockListener(LockListener lockListener) {
        this.lockListener = lockListener;
    }

    public void setSwitchListener(SwitchListener switchListener) {
        this.switchListener = switchListener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull SoftWareViewHolder holder, @SuppressLint("RecyclerView") int position) {
        AppEntity entityApp = entityApp1.get(position);
        String label = entityApp.getApplabel();
        holder.itemAppBinding.appLabel.setText(label);
        Drawable icon = AppManager.getIcon(mCon, entityApp.getApppackageName());
        if (icon != null) {
            holder.itemAppBinding.appLogo.setImageDrawable(icon);
        }
        holder.itemAppBinding.appLocked.setSelected(entityApp.isApplocked());
        holder.itemAppBinding.appLocked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keyid = getkey();
                keyid++;
                keyeditor = keysp.edit();
                if (keyid % 5 == 0) {
                    keyeditor.putInt("key", keyid);
                    keyeditor.apply();
                    ATInterstitial mInterstitialAd = Mytool.onCache(ads);
                    if (mInterstitialAd == null) {
                        initEvent(label, entityApp, position, holder.itemAppBinding.appLocked);
                    } else {
                        Mytool.setCallback(mInterstitialAd, new Adcallback() {
                            @Override
                            public void onShowFail(AdError ad) {
                                initEvent(label, entityApp, position, holder.itemAppBinding.appLocked);
                            }

                            @Override
                            public void onAdHidden() {
                                initEvent(label, entityApp, position, holder.itemAppBinding.appLocked);
                            }
                        });
                        mInterstitialAd.show(mCon);
                    }

                } else {
                    keyeditor.putInt("key", keyid);
                    keyeditor.apply();
                    initEvent(label, entityApp, position, holder.itemAppBinding.appLocked);
                }
            }
        });

    }



    private void initEvent(String label, AppEntity entityApp, int position, View view) {
        Datamanager.setOpCount();
        AlertDialog alertDialog2 = new AlertDialog.Builder(mCon)
                .setTitle("Prompt")
                .setMessage("Are you sure " + label + "?")
                .setIcon(R.mipmap.intologo)
                .setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        boolean selected = view.isSelected();
                        view.setSelected(!selected);
                        boolean selectedNew = view.isSelected();
                        entityApp.setApplocked(selectedNew);
                        SoftWareLockRoom.updateRoom(entityApp);
                        String name2;
                        if (selectedNew) {
                            if (!mLockList) {
                                entityApp1.remove(entityApp);
                                notifyItemRemoved(position);
                            } else {

                            }
                            name2 = String.format(mCon.getString(R.string.lock_app), label);
                            setbacklistener(name2);
                        } else {
                            name2 = String.format(mCon.getString(R.string.unlock_app), label);
                            setbacklistener(name2);
                            if (mLockList) {
                                entityApp1.remove(entityApp);
                                notifyItemRemoved(position);
                            }
                        }
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //holder.itemAppBinding.appLocked.setSelected(!selected);
                    }
                })
                .create();
        alertDialog2.show();
    }

    public static int getkey() {
        return keysp.getInt("key", 0);
    }

    private void setbacklistener(String softname) {
        if (lockListener != null) {
            lockListener.onSwitch(softname);
        }
    }


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

    public void setEntityApps(List<AppEntity> apps) {
        entityApp1 = apps;
        notifyDataSetChanged();

    }

    static class SoftWareViewHolder extends RecyclerView.ViewHolder {
        private ItemAppBinding itemAppBinding;

        public SoftWareViewHolder(@NonNull ItemAppBinding itemView) {
            super(itemView.getRoot());
            itemAppBinding = itemView;
        }
    }
}
