package com.usoftchina.bi.server.service.dashboard;

import com.alibaba.fastjson.JSONObject;
import com.usoftchina.bi.core.utils.EncryUtil;
import com.usoftchina.bi.server.dao.chart.ChartsConfigMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.usoftchina.bi.server.model.bo.Screen;
import com.usoftchina.bi.server.model.po.ChartsConfigToDash;
import com.usoftchina.bi.core.base.RepCode;
import com.usoftchina.bi.core.base.RepEntity;
import com.usoftchina.bi.core.base.TestPage;
import com.usoftchina.bi.server.model.po.TokenData;
import com.usoftchina.bi.server.model.po.User;
import com.usoftchina.bi.server.model.vo.configVo.*;
import com.usoftchina.bi.server.model.vo.dataVo.PopAndIndDataInfo;
import com.usoftchina.bi.server.service.chart.*;
import com.usoftchina.bi.server.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
看板中调用生成图表
 */
@Service
public class DashboardsToChartsUtilService {
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    ShowHistogramService showHistogramService;
    @Autowired
    ShowIndividualService showIndividualService;
    @Autowired
    ShowLineService showLineService;
    @Autowired
    ShowPieService showPieService;
    @Autowired
    ShowPopulationService showPopulationService;
    @Autowired
    ShowScatterService showScatterService;
    @Autowired
    private UserService userService;

    public RepEntity getChartsInDash(ChartsToDashInfo chartsToDashInfo, String token) throws SQLException {
        if (StringUtils.isEmpty(token) || "null".equals(token)) {
            User user = userService.getUserById(chartsToDashInfo.getDashboardCreatorId()).getData();
            LoginInfo loginInfo = new LoginInfo(user.getUserName(), EncryUtil.decryptPassword(user.getPassWord()));
            token = userService.login(loginInfo).getData().getToken();
        }

        int chartId = chartsToDashInfo.getChartId();

        //拿到图表配置
        ChartsConfigToDash chartsConfigToDash = chartsConfigMapper.getChartConfigToDash(chartId);
        if (chartsConfigToDash == null || "".equals(chartsConfigToDash)){
            return new RepEntity(RepCode.Null);
        }
        String chartType = chartsConfigToDash.getChartType();
        String fetchConfig = chartsConfigToDash.getFetchConfig();
        String styleConfig = chartsConfigToDash.getStyleConfig();
        if (fetchConfig == null || "".equals(fetchConfig)){
            return new RepEntity(RepCode.Null);
        }
        JSONObject.parseArray("["+fetchConfig+"]").get(0);
        //判断图表类型
        List<Screen> filters = chartsToDashInfo.getFilters();
        if ("Pie".equals(chartType)){
            return getPie(fetchConfig, token, filters, chartId, styleConfig);
        }else if ("Histogram".equals(chartType)){
            return getHistogram(fetchConfig, token, filters, chartId, styleConfig);

        }else if ("Line".equals(chartType)){
            return getLine(fetchConfig, token, filters, chartId, styleConfig);

        }else if ("population".equals(chartType)){
            return getPopulation(fetchConfig, token, filters, chartId, styleConfig);

        }else if ("individual".equals(chartType)){
            return getIndividual(fetchConfig, token, filters, chartsToDashInfo.getTestPage(), chartId, styleConfig);

        }else if("scatter".equals(chartType)){
            return getScatter(fetchConfig, token, filters, chartId, styleConfig);
        }
        return new RepEntity(RepCode.success);
    }

    public RepEntity getHistogram(String fetchConfig, String token, List<Screen> filters, int chartId, String styleConfig){
        HistogramConfigInfo histogramConfigInfo = new HistogramConfigInfo();
        try {
            histogramConfigInfo = objectMapper.readValue(fetchConfig,HistogramConfigInfo.class);
            histogramConfigInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showHistogramService.showHistogram(histogramConfigInfo, token, chartId);
    }

    public RepEntity getScatter(String fetchConfig, String token, List<Screen> filters, int chartId, String styleConfig){
            ScatterConfigInfo scatterConfigInfo = new ScatterConfigInfo();
            try {
                scatterConfigInfo = objectMapper.readValue(fetchConfig,ScatterConfigInfo.class);
                scatterConfigInfo.setFilters(filters);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return showScatterService.showScatter(scatterConfigInfo, token, chartId, styleConfig);
    }

    public RepEntity getLine(String fetchConfig, String token, List<Screen> filters, int chartId, String styleConfig){
        LineConfigInfo lineConfigInfo = new LineConfigInfo();
        try {
            lineConfigInfo = objectMapper.readValue(fetchConfig,LineConfigInfo.class);
            lineConfigInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showLineService.showLine(lineConfigInfo, token, chartId, styleConfig);
    }

    public RepEntity getPopulation(String fetchConfig, String token, List<Screen> filters, int chartId, String styleConfig) throws SQLException {
        PopulationInfo populationInfo = new PopulationInfo();
        try {
            populationInfo = objectMapper.readValue(fetchConfig,PopulationInfo.class);
            populationInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RepEntity<PopAndIndDataInfo> repEntity = showPopulationService.showPopulation(populationInfo, token, chartId);
        repEntity.getData().setStyleConfig(styleConfig);
        return repEntity;
    }

    public RepEntity getIndividual(String fetchConfig, String token, List<Screen> filters, TestPage testPage, int chartId, String styleConfig) throws SQLException {
        IndividualConfigInfo individualConfigInfo = new IndividualConfigInfo();
        try {
            individualConfigInfo = objectMapper.readValue(fetchConfig,IndividualConfigInfo.class);
            individualConfigInfo.setFilters(filters);
            individualConfigInfo.setTestPage(testPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RepEntity<PopAndIndDataInfo> repEntity = showIndividualService.showIndividual(individualConfigInfo, token, chartId);
        repEntity.getData().setStyleConfig(styleConfig);
        return repEntity;
    }

    public RepEntity getPie(String fetchConfig, String token, List<Screen> filters, int chartId, String styleConfig){
        PieConfigInfo pieConfigInfo = new PieConfigInfo();
        try {
            pieConfigInfo = objectMapper.readValue(fetchConfig,PieConfigInfo.class);
            pieConfigInfo.setFilters(filters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showPieService.showPie(pieConfigInfo, token, chartId, styleConfig);
    }

}
