package com.dxhy.customs.model;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;

import cn.hutool.core.codec.Base64;
import lombok.Data;

/**
 * @author dxhy
 */

@Data
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class ResultData<T> {
    private List<T> datalist;
    private Map<String, List<String>> titlelist;
    private long total = 0;
    private String hjsl = "0";
    private String hjje = "0";
    private String hjse = "0";
    private Map<String, Object> data;
    private Boolean listLoading;
    private Boolean selection;
    private Boolean numberSerial;
    private List<Boolean> titlefixed;
    private List<String> titlewidth;
    private List<Boolean> iconfixed;
    private List<Boolean> inputfixed;
    private List<Boolean> showOverflowTooltip;
    private Map<String, Object> operating;

    public static boolean isEmpty(Page result) {
        return result != null && CollectionUtils.isNotEmpty(result.getRecords());
    }

    public ResultData putTitleList(String[] titlelist, String[] titleProp) {
        return this.putTitleList(Arrays.asList(titlelist), Arrays.asList(titleProp));
    }

    public ResultData putTitleList(List<String> titlelist, List<String> titleProp) {
        this.titlelist = new HashMap<String, List<String>>(16);
        this.titlelist.put("titlelist", titlelist);
        this.titlelist.put("titleprop", titleProp);
        return this;
    }

    public ResultData putTabulateMenu(JSONObject tabulateMenu) {
        this.titlelist = JSON.parseObject(tabulateMenu.getJSONObject("titlelist").toJSONString(), Map.class);
        this.listLoading = tabulateMenu.getBoolean("listLoading");
        this.selection = tabulateMenu.getBoolean("selection");
        this.numberSerial = tabulateMenu.getBoolean("numberSerial");
        this.titlefixed = JSON.parseObject(tabulateMenu.getJSONArray("titlefixed").toJSONString(), List.class);
        this.titlewidth = JSON.parseObject(tabulateMenu.getJSONArray("titlewidth").toJSONString(), List.class);
        this.iconfixed = JSON.parseObject(tabulateMenu.getJSONArray("iconfixed").toJSONString(), List.class);
        this.inputfixed = JSON.parseObject(tabulateMenu.getJSONArray("inputfixed").toJSONString(), List.class);
        this.showOverflowTooltip =
            JSON.parseObject(tabulateMenu.getJSONArray("showOverflowTooltip").toJSONString(), List.class);
        this.operating = JSON.parseObject(tabulateMenu.getJSONObject("operating").toJSONString(), Map.class);
        return this;
    }

    public ResultData putData(Map<String, Object> data) {
        this.data = data;
        return this;
    }

    public ResultData getTjData(Map map) {
        if (map == null) {
            return this;
        }

        if (map.get("hjsl") != null && !"".equals(map.get("hjsl"))) {
            this.hjsl = map.get("hjsl") + "";
        }

        if (map.get("hjje") != null && !"".equals(map.get("hjje"))) {
            this.hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(map.get("hjje").toString()));
        }

        if (map.get("hjse") != null && !"".equals(map.get("hjse"))) {
            this.hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(map.get("hjse").toString()));
        }

        return this;
    }

    public ResultData getPageData(Page result) {
        if (result == null) {
            this.datalist = null;
            this.total = 0;
            return this;
        }
        this.datalist = result.getRecords();
        this.total = result.getTotal();
        return this;
    }

    public ResultData setDataList(List<T> list) {
        this.datalist = list;
        return this;
    }

    public String toJsonBase() {
        return Base64.encode(JSONObject.toJSONString(this));
    }
}
