package yem.base.common.module.sso;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.form.chart.ItemValue;
import kd.bos.form.chart.PieChart;
import kd.bos.form.chart.PointLineChart;
import kd.bos.form.plugin.AbstractFormPlugin;
import yem.base.common.utils.ChartUtil;
import yem.base.common.utils.PieChartHelp;
import yem.base.common.utils.PointLineChartHelper;

import java.math.BigDecimal;
import java.util.*;

/**
 * @auther zhangh
 * 额度使用分析
 */
public class UsageAnalysisFormPlugin extends AbstractFormPlugin{

    @Override
    public void afterBindData(EventObject e) {
        pointLine();
        pieLineChart();
    }

    public void pieLineChart() {
        PieChart yem_piechartap = this.getControl("yem_piechartap");
        List<ItemValue> dataPie = getDataPie(this.getView());
        PieChartHelp.drawChart(yem_piechartap, dataPie);
    }

    public List<ItemValue> getDataPie(IFormView view) {
        List<ItemValue> list = new ArrayList<>();
        DynamicObject yem_itemclassfield = (DynamicObject) view.getModel().getValue("yem_itemclassfield");
        if(yem_itemclassfield == null){
            list.add(new ItemValue("占用金额", 451, ChartUtil.getRandColor()));
            list.add(new ItemValue("释放金额", 741, ChartUtil.getRandColor()));
            list.add(new ItemValue("欠款金额", 300, ChartUtil.getRandColor()));
        }else{
            list.add(new ItemValue("占用金额", 140, ChartUtil.getRandColor()));
            list.add(new ItemValue("释放金额", 287, ChartUtil.getRandColor()));
            list.add(new ItemValue("欠款金额", 150, ChartUtil.getRandColor()));
        }

        return list;
    }

    public void pointLine() {
        PointLineChart yem_pointlinechartap = this.getControl("yem_pointlinechartap");
        Map<String, Map<String, BigDecimal>> dataPoint = getDataPoint(this.getView());
        PointLineChartHelper.drawChart(yem_pointlinechartap, dataPoint, "月份", "金额");
    }

    private Map<String, Map<String, BigDecimal>> getDataPoint(IFormView view) {
        DynamicObject yem_itemclassfield = (DynamicObject) view.getModel().getValue("yem_itemclassfield");
        Map<String, Map<String, BigDecimal>> map = new HashMap<>();
        if (yem_itemclassfield == null){
            Map<String,BigDecimal> data = new HashMap<>();
            data.put("占用金额", new BigDecimal(7900));
            data.put("释放金额", new BigDecimal(7900));
            data.put("欠款金额", new BigDecimal(0));
            map.put("02", data);

            Map<String,BigDecimal> data1 = new HashMap<>();
            data1.put("占用金额", new BigDecimal(15830));
            data1.put("释放金额", new BigDecimal(24563));
            data1.put("欠款金额", new BigDecimal(8733));
            map.put("03", data1);

            Map<String,BigDecimal> data2 = new HashMap<>();
            data2.put("占用金额", new BigDecimal(14500));
            data2.put("释放金额", new BigDecimal(26841));
            data2.put("欠款金额", new BigDecimal(12341));
            map.put("04", data2);

            Map<String,BigDecimal> data3 = new HashMap<>();
            data3.put("占用金额", new BigDecimal(45120));
            data3.put("释放金额", new BigDecimal(46200));
            data3.put("欠款金额", new BigDecimal(1080));
            map.put("05", data3);

            Map<String,BigDecimal> data4 = new HashMap<>();
            data4.put("占用金额", new BigDecimal(36500));
            data4.put("释放金额", new BigDecimal(45000));
            data4.put("欠款金额", new BigDecimal(8500));
            map.put("06", data4);

            Map<String,BigDecimal> data5 = new HashMap<>();
            data5.put("占用金额", new BigDecimal(34500));
            data5.put("释放金额", new BigDecimal(34500));
            data5.put("欠款金额", new BigDecimal(0));
            map.put("07", data5);

            Map<String,BigDecimal> data6 = new HashMap<>();
            data6.put("占用金额", new BigDecimal(37500));
            data6.put("释放金额", new BigDecimal(38500));
            data6.put("欠款金额", new BigDecimal(1000));
            map.put("08", data6);

            Map<String,BigDecimal> data7 = new HashMap<>();
            data7.put("占用金额", new BigDecimal(26890));
            data7.put("释放金额", new BigDecimal(27000));
            data.put("欠款金额", new BigDecimal(110));
            map.put("09", data7);

            Map<String,BigDecimal> data8 = new HashMap<>();
            data8.put("占用金额", new BigDecimal(24750));
            data8.put("释放金额", new BigDecimal(26802));
            data8.put("欠款金额", new BigDecimal(2052));
            map.put("10", data8);
        }else{
            Map<String,BigDecimal> data = new HashMap<>();
            data.put("占用金额", new BigDecimal(150));
            data.put("释放金额", new BigDecimal(450));
            data.put("欠款金额", new BigDecimal(300));
            map.put("02", data);

            Map<String,BigDecimal> data1 = new HashMap<>();
            data1.put("占用金额", new BigDecimal(750));
            data1.put("释放金额", new BigDecimal(890));
            data1.put("欠款金额", new BigDecimal(140));
            map.put("03", data1);

            Map<String,BigDecimal> data2 = new HashMap<>();
            data2.put("占用金额", new BigDecimal(1800));
            data2.put("释放金额", new BigDecimal(2460));
            data2.put("欠款金额", new BigDecimal(660));
            map.put("04", data2);

            Map<String,BigDecimal> data3 = new HashMap<>();
            data3.put("占用金额", new BigDecimal(4800));
            data3.put("释放金额", new BigDecimal(9800));
            data3.put("欠款金额", new BigDecimal(5000));
            map.put("05", data3);

            Map<String,BigDecimal> data4 = new HashMap<>();
            data4.put("占用金额", new BigDecimal(4500));
            data4.put("释放金额", new BigDecimal(7625));
            data4.put("欠款金额", new BigDecimal(3125));
            map.put("06", data4);

            Map<String,BigDecimal> data5 = new HashMap<>();
            data5.put("占用金额", new BigDecimal(2400));
            data5.put("释放金额", new BigDecimal(2400));
            data5.put("欠款金额", new BigDecimal(0));
            map.put("07", data5);

            Map<String,BigDecimal> data6 = new HashMap<>();
            data6.put("占用金额", new BigDecimal(2400));
            data6.put("释放金额", new BigDecimal(2600));
            data6.put("欠款金额", new BigDecimal(200));
            map.put("08", data6);

            Map<String,BigDecimal> data7 = new HashMap<>();
            data7.put("占用金额", new BigDecimal(3800));
            data7.put("释放金额", new BigDecimal(4680));
            data.put("欠款金额", new BigDecimal(880));
            map.put("09", data7);

            Map<String,BigDecimal> data8 = new HashMap<>();
            data8.put("占用金额", new BigDecimal(4500));
            data8.put("释放金额", new BigDecimal(4896));
            data8.put("欠款金额", new BigDecimal(396));
            map.put("10", data8);
        }

        return map;

    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String name = e.getProperty().getName();
        switch (name){
            case "yem_itemclassfield":
                pointLine();
                pieLineChart();
                break;
        }
    }
}