package com.yutou.jianr_mg.Adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Data.TMod;
import com.yutou.jianr_mg.Databases.ModDatabase;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.ModUtils;
import com.yutou.jianr_mg.Tools.Utils;
import com.yutou.jianr_mg.views.ModActivity;

import java.util.List;

import me.drakeet.materialdialog.MaterialDialog;

public class DownloadItemAdapter extends BaseAdapter {
    private Context context;
    private List<TMod> list;
    private ImageLoader imageLoader;

    public DownloadItemAdapter(Context context, List<TMod> list) {
        this.context = context;
        this.list = list;
        imageLoader= Utils.initImageLoader(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Items items;
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_download,null);
            items=new Items();
            items.delete=view.findViewById(R.id.delete);
            items.install=view.findViewById(R.id.install);
            items.icon=view.findViewById(R.id.image);
            items.title=view.findViewById(R.id.title);
            view.setTag(items);
        }else{
            items= (Items) view.getTag();
        }
        if(list.get(i)==null)
            return  view;
        items.title.setText(list.get(i).getTitle());
        imageLoader.displayImage(AppData.appConfig.getDownloadhome()+list.get(i).getIcon(),items.icon);
        items.delete.setOnClickListener(v -> {
            final MaterialDialog dialog=new MaterialDialog(context);
            dialog.setTitle("提示");
            dialog.setMessage("删除这个内容？");
            dialog.setNegativeButton("我手滑了", v12 -> dialog.dismiss());
            dialog.setPositiveButton("是的", v1 -> {
                Utils.deleteFiles(Utils.getAppPath()+"/"+list.get(i).getId());
                ModDatabase database=ModDatabase.init();
                database.deleteMod(list.get(i));
                Utils.toast(context,"删除完成");
                list.remove(i);
                notifyDataSetChanged();
                dialog.dismiss();
            });
            dialog.show();
        });
        items.install.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModUtils mod=ModUtils.init(context);
                mod.installMod(list.get(i), null);
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ModActivity.class);
                intent.putExtra("mod", JSON.toJSONString(list.get(i)));
                context.startActivity(intent);
            }
        });
        return view;
    }
    private class Items{
        ImageView icon;
        TextView title;
        Button install,delete;
    }
}
