package com.yutou.qqbot.bilibili;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.bilibili.api.MangaApi;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.interfaces.ObjectInterface;
import com.yutou.qqbot.utlis.HttpTools;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class BiliBiliManga {
    public BiliBiliManga(long qq) {
        biliUtils = BiliBiliUtils.getInstance(qq);
    }

    public static JSONObject sign() {
        JSONObject body = new JSONObject();
        body.put("platform", "android");
        return BiliBiliUtils.getInstance(QQBotManager.defQQ).http_post(MangaApi.SIGN, HttpTools.toUrlParams(body));
    }

    private static JSONObject getListProductDate() {
        return BiliBiliUtils.getInstance(QQBotManager.defQQ).http_post(MangaApi.LIST_PRODUCT_DATE, "");
    }

    private static boolean isPayMission = false;
    private static Product missionProduct = new Product();
    private static Timer mission = null;
    private static BiliBiliUtils biliUtils = null;
    private ObjectInterface anInterface = null;

    public void addInterface(ObjectInterface objectInterface) {
        anInterface = objectInterface;
    }

    public static boolean isPayMission() {
        return isPayMission;
    }

    public static String getMission() {
        return missionProduct + " 兑换数量:" + missionProduct.getPayAmount();
    }

    public static List<Product> getListProduct() {
        List<Product> list = new ArrayList<>();
        JSONObject product = getListProductDate();
        if (product.getInteger("code") == 0) {
            JSONArray array = product.getJSONArray("data");
            for (Object o : array) {
                JSONObject data = (JSONObject) o;
                Product item = new Product();
                item.setId(data.getInteger("id"));
                item.setTitle(data.getString("title"));
                item.setAmount(data.getInteger("amount"));
                item.setRemain_amount(data.getInteger("remain_amount"));
                item.setReal_cost(data.getInteger("real_cost"));
                list.add(item);
            }
        }
        return list;
    }

    public static int getMyPoint() {
        JSONObject user = biliUtils.http_post(MangaApi.USER_POINT, "");
        if (user != null && user.getInteger("code") == 0) {
            return user.getJSONObject("data").getInteger("point");
        }
        return 0;
    }

    public JSONObject startPayMission(int id, int num) {
        JSONObject json = new JSONObject();
        if (isPayMission) {
            json.put("code", 2);
            json.put("msg", "任务正在进行:" + getMission());
            return json;
        }
        if (biliUtils == null) {
            json.put("code", -1);
            json.put("msg", "B站未登录");
            return json;
        }
        int userPoint = getMyPoint();
        List<Product> list = getListProduct();
        missionProduct = null;
        for (Product product : list) {
            if (product.getId() == id) {
                missionProduct = product;
                break;
            }
        }
        if (missionProduct == null) {
            json.put("code", -1);
            json.put("msg", "未找到商品，可能id有误 id:" + id);
            return json;
        }
        if (num == -1) {
            num = 99999;
        }
        int userPointNum = userPoint / missionProduct.getReal_cost();
        num = Math.min(num, userPointNum);
        if (num < missionProduct.getRemain_amount()) {
            num = missionProduct.getRemain_amount();
        }

        missionProduct.setPayAmount(num);
        JSONObject data = new JSONObject();
        data.put("product_id", id);
        data.put("product_num", num);
        data.put("point", num * missionProduct.getReal_cost());
        startPayMission(data);
        isPayMission = true;

        if (num == 0) {
            json.put("code", 3);
            json.put("msg", "商品无货，正在抢购");
        } else {
            json.put("code", 0);
            json.put("msg", "任务创建成功：" + missionProduct + " 兑换数量:" + num);
        }
        return json;
    }

    private void startPayMission(JSONObject data) {
        if (mission != null) {
            mission.cancel();
        }
        mission = new Timer();
        mission.schedule(new TimerTask() {
            @Override
            public void run() {
                JSONObject post = biliUtils.http_post(MangaApi.PAY_MISSION, HttpTools.toUrlParams(data));
                if (post == null) {
                    anInterface.out("网络请求失败，请查看日志");
                    cancel();
                    return;
                }
                if (post.getInteger("code") == 0) {
                    anInterface.out("兑换成功，任务已取消");
                    isPayMission = false;
                    cancel();
                } else {
                    anInterface.out("[" + post.getInteger("code") + "]" + post.getString("msg"));
                }
            }
        }, 0, 1000);
    }

    public void stopPayMission() {
        if (isPayMission) {
            isPayMission = false;
            if (mission != null) {
                mission.cancel();
            }
        }
    }

    public static void main(String[] args) {
      //  System.out.println(BiliBiliManga.sign());
        BiliBiliUtils.getInstance(583819556L).sendLiveDanmu(33989,"学学这个");
    }

    @Data
    public static class Product {
        String title;
        int id;
        int remain_amount;
        int real_cost;
        int amount;
        int payAmount;

        @Override
        public String toString() {
            return "[ " + id + " ] " + title + " (" + remain_amount + "/" + amount + ") $" + real_cost;
        }
    }
}
