package com.yutou.jianr_mg.views;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.umeng.analytics.MobclickAgent;
import com.yutou.jianr_mg.Adapters.DownloadItemAdapter;
import com.yutou.jianr_mg.Data.TMod;
import com.yutou.jianr_mg.Databases.ModDatabase;
import com.yutou.jianr_mg.Interfaces.HttpInterface;
import com.yutou.jianr_mg.Network.HttpApi;
import com.yutou.jianr_mg.Network.HttpUtils;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.ModUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

import me.drakeet.materialdialog.MaterialDialog;

public class DownloadListActivity extends AppCompatActivity {
    private Context context;
    private ListView listView;
    private List<TMod> mods, mods_;
    private Handler handler;
    private ModDatabase database;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_list);
        this.context = this;
        handler = new Handler();
        this.database = ModDatabase.init();
        getDownloadFileList();
        initViews();
    }

    private void getDownloadFileList() {
        JSONObject json = new JSONObject();
        mods_ = database.getDownloadModIds();
        try {
            JSONArray array = new JSONArray();
            for (TMod mod : mods_) {
                array.put(mod.getId());
            }
            json.put("ids", array);
            HttpUtils.post(HttpApi.HOME_URL + HttpApi.MOD_MODS, json, new HttpInterface() {
                @Override
                public void httpGetData(String string, int code) {
                    try {
                        JSONObject json = new JSONObject(string);
                        if (json.getInt("code") != 100) {
                            return;
                        }
                        mods = JSON.parseArray(json.getJSONArray("data").toString(), TMod.class);
                        handler.post(() -> {
                            listView.setAdapter(new DownloadItemAdapter(context, mods));
                            String title = "";
                            List<TMod> ids = new ArrayList<>(mods_.size());
                            for (TMod mod : mods) {
                                for (TMod tMod : mods_) {
                                    if (tMod.getTitle().equals(mod.getTitle()) && tMod.getModversion() < mod.getModversion()) {
                                        System.out.println(tMod.getTitle() + " " + tMod.getModversion() + " " + mod.getTitle() + " " + mod.getModversion());
                                        title += "\n" + tMod.getTitle();
                                        ids.add(mod);
                                    }
                                }
                            }
                            if (!ids.isEmpty())
                                showUploadModDialog(title, ids);
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void httpError(Exception e) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void showUploadModDialog(String title, List<TMod> mods) {
        MaterialDialog dialog = new MaterialDialog(context);
        dialog.setTitle("Mod升级");
        dialog.setMessage("你有以下mod可以进行升级：" + title + "\n是否进行升级？");
        dialog.setNegativeButton("不用了", view -> dialog.dismiss());
        dialog.setPositiveButton("好的", view -> {
            ModUtils utils = ModUtils.init(context);
            for (TMod mod : mods) {
                utils.installMod(mod, null);
            }
            dialog.dismiss();
        });
        dialog.show();
    }

    private void initViews() {
        listView = findViewById(R.id.listView);

    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

}
