package com.tool.applockpro.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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


import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.tool.applockpro.ApplockSkin;
import com.tool.applockpro.R;
import com.tool.applockpro.data.MyDataBase;
import com.tool.applockpro.data.Mydata;
import com.tool.applockpro.tool.AdActionListener;
import com.tool.applockpro.tool.Adcallback;
import com.tool.applockpro.tool.Mytools;

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

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppVH> {
    private Context mycontext;
    private List<Mydata> mydataList = new ArrayList<>();
    private PackageManager packageManager;
    private Activity mactivity;
    private List<ATInterstitial> ads;
    private int a = 0;

    @SuppressLint("NotifyDataSetChanged")
    public AppAdapter(Context context, Activity activity) {
        ads = ApplockSkin.lodAd();
        mycontext = context;
        mactivity = activity;
        packageManager = context.getPackageManager();

        a = ApplockSkin.keyad;
    }

    public void update(List<Mydata> list) {
        mydataList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AppVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mycontext).inflate(R.layout.item_app, parent, false);
        return new AppVH(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull AppVH holder, @SuppressLint("RecyclerView") int position) {
        Mydata mydata = mydataList.get(position);
        String appname = mydata.getAppName();
        boolean lock = mydata.isLock();
        holder.switchCompat.setSelected(lock);
        holder.lockView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApplockSkin.keyad++;
                if (ApplockSkin.keyad % 5 == 0 || ApplockSkin.keyad == 1) {
                    showAd(new AdActionListener() {
                        @Override
                        public void onAction() {
                            String strLock = holder.switchCompat.isSelected() ? "unlock" : "lock";
                            new AlertDialog.Builder(mycontext)
                                    .setTitle("Tips")
                                    .setMessage("Are you sure you want to " + strLock + " " + appname)
                                    .setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (holder.switchCompat.isSelected()) {
                                                holder.switchCompat.setSelected(false);
                                            } else {
                                                holder.switchCompat.setSelected(true);
                                            }
                                            Mytools.runIO(new Runnable() {
                                                @Override
                                                public void run() {
                                                    mydata.setLock(holder.switchCompat.isSelected());
                                                    MyDataBase.getINSTANCE().mydao().update(mydata);

                                                }
                                            });
                                            String s;
                                            if (holder.switchCompat.isSelected()) {
                                                s = String.format(mycontext.getString(R.string.text_locked), appname);
                                            } else {
                                                s = String.format(mycontext.getString(R.string.text_unlocked), appname);
                                            }
                                            Toast.makeText(mycontext, s, Toast.LENGTH_SHORT).show();
                                        }
                                    })
                                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {

                                        }
                                    })
                                    .show();

                        }
                    });
                } else {

                    if (holder.switchCompat.isSelected()) {
                        holder.switchCompat.setSelected(false);
                    } else {
                        holder.switchCompat.setSelected(true);
                    }
                    Mytools.runIO(new Runnable() {
                        @Override
                        public void run() {
                            mydata.setLock(holder.switchCompat.isSelected());
                            MyDataBase.getINSTANCE().mydao().update(mydata);

                        }
                    });
                    String s;
                    if (holder.switchCompat.isSelected()) {
                        s = String.format(mycontext.getString(R.string.text_locked), appname);
                    } else {
                        s = String.format(mycontext.getString(R.string.text_unlocked), appname);
                    }


                    Toast.makeText(mycontext, s, Toast.LENGTH_SHORT).show();
                }


            }

        });
        holder.textView.setText(mydata.getAppName());
        try {
            Drawable appLogo = getLogo(mydata.getPackageName());
            holder.imageView.setImageDrawable(appLogo);
        } catch (PackageManager.NameNotFoundException e) {

        }
    }

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

    private Drawable getLogo(String packageName) throws PackageManager.NameNotFoundException {
        ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
        return packageManager.getApplicationIcon(appInfo);
    }

    public static class AppVH extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView textView;
        private ImageView switchCompat;
        private CardView lockView;

        public AppVH(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imlogo);
            textView = itemView.findViewById(R.id.name);
            switchCompat = itemView.findViewById(R.id.app_switch);
            lockView = itemView.findViewById(R.id.lock);
        }

    }


    private void showAd(AdActionListener listener) {
        ATInterstitial mInterstitialAd = Mytools.onCache(ads);
        if (mInterstitialAd == null) {
            listener.onAction();
        } else {
            Mytools.setCallback(mInterstitialAd, new Adcallback() {
                @Override
                public void onShowFail(AdError ad) {
                    listener.onAction();
                }

                @Override
                public void onAdHidden() {
                    listener.onAction();
                }
            });
            mInterstitialAd.show(mactivity);
        }
    }


}
