package com.tool.applockpro.adapter;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;


import com.tool.applockpro.R;
import com.tool.applockpro.data.MyDataBase;
import com.tool.applockpro.data.Mydata;
import com.tool.applockpro.tool.Mytools;

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

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppVH> {
    private Context mycontext;
    private List<Mydata> mydataList = new ArrayList<>();
    private PackageManager packageManager;
    private boolean mtost = true;
    public static SharedPreferences keysp;
    public static SharedPreferences.Editor keyeditor;
    private int keyid;
    private ObjectAnimator animator1, animator2;
    private Activity activity;


    @SuppressLint("NotifyDataSetChanged")
    public AppAdapter(Context context, List<Mydata> list) {
        mydataList = list;
        mycontext = context;
        packageManager = context.getPackageManager();
        keysp = mycontext.getSharedPreferences("key", Context.MODE_PRIVATE);
        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();
//        Log.e("zzj", mydata.getAppName() + "------mydata.isLock-----" + lock);
        holder.switchCompat.setSelected(lock);
        holder.lock.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();
//                    Log.e("zzj", String.valueOf(keyid));
                    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();
                } else {
                    keyeditor.putInt("key", keyid);
                    keyeditor.apply();
                    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) {

        }
    }


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

    @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 AppCompatImageView switchCompat;
        private ConstraintLayout lock;

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

    }

}
