package com.yutou.jianr_mg.Adapters;

import android.content.Context;
import android.content.Intent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yutou.jianr_mg.Application;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Data.MAppTools;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.JianRUtils;
import com.yutou.jianr_mg.Tools.Utils;
import com.yutou.jianr_mg.views.BaseActivity;

import cn.lemon.view.adapter.BaseViewHolder;
import cn.lemon.view.adapter.RecyclerAdapter;
import me.drakeet.materialdialog.MaterialDialog;

public class MainUtilsAdapter extends RecyclerAdapter<MAppTools> {
    private ImageLoader imageLoader;

    public MainUtilsAdapter(Context context) {
        super(context);
        imageLoader = Utils.initImageLoader(getContext());
    }

    @Override
    public BaseViewHolder<MAppTools> onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(parent, R.layout.item_mg_home);
    }

    private class MyViewHolder extends BaseViewHolder<MAppTools> {
        private ImageView icon;
        private TextView name;

        MyViewHolder(ViewGroup parent, int layoutId) {
            super(parent, layoutId);
            icon = findViewById(R.id.image);
            name = findViewById(R.id.title);
        }

        @Override
        public void setData(MAppTools data) {
            super.setData(data);
            if (data.getImage().equals("#")) {
                icon.setImageResource(R.mipmap.icon_logo);
            } else
                imageLoader.displayImage(data.getImage(), icon);
            name.setText(data.getName());
        }

        @Override
        public void onItemViewClick(MAppTools data) {
            super.onItemViewClick(data);
            if (data.getMainFunction().equals("#")) {
                return;
            }

            String type = data.getMainFunction().split("#")[1];
            String main = data.getMainFunction().split("#")[0];
            try {

                Intent intent = null;
                if (!type.equals("localActivity")) {
                    intent = new Intent(getContext(), Application.application.getClassLoader().loadClass(main));
                    if (data.getParameter() != null) {
                        intent.putExtra("parameter", data.getParameter());
                    }
                }
                switch (type) {
                    case "activity":
                        getContext().startActivity(intent);
                        break;
                    case "service":
                        getContext().startService(intent);
                        break;
                    case "localActivity":
                        System.out.println("数据：" + data.toString());
                        intent = new Intent(getContext(), BaseActivity.class);
                        intent.putExtra("activityName", main);
                        intent.putExtra("jar", data.getUrl().split("/")[data.getUrl().split("/").length - 1]);
                        intent.putExtra("name", data.getName());
                        intent.putExtra("url", data.getUrl());
                        System.out.println("插件数据：" + data.getUrl().split("/")[data.getUrl().split("/").length - 1]);
                        if (data.getParameter() != null) {
                            intent.putExtra("parameter", data.getParameter());
                        }
                        getContext().startActivity(intent);
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
                Utils.toast(getContext(), "启动失败，请升级至最新版本或联系管理员");
            }
        }

        @Override
        public void onItemViewLongClick(MAppTools data) {
            super.onItemViewLongClick(data);
            String type = data.getMainFunction().split("#")[1];
            if(AppData.plugsin.containsKey(data.getPluginName()+".jar")) {
                if (type.equals("localActivity")) {
                    MaterialDialog dialog = new MaterialDialog(getContext());
                    dialog.setTitle("卸载插件");
                    dialog.setMessage("确定删除" + data.getName() + "插件？");
                    dialog.setNegativeButton("取消", v -> dialog.dismiss());
                    dialog.setPositiveButton("确定", v -> {
                        try {
                            AppData.plugsin.remove(data.getPluginName() + ".jar");
                            Utils.removePlugins(data.getPluginName());
                            Utils.toast(getContext(), "卸载成功，重启生效");
                            dialog.dismiss();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                    dialog.show();

                }
            }
        }
    }
}
