package data

import (
	"encoding/json"
	"errors"
	"eta/eta_api/models/data_manage"
	"eta/eta_api/models/data_manage/chart_theme"
	"eta/eta_api/utils"
	"fmt"
	"github.com/shopspring/decimal"
	"math"
	"time"
)

// GetThemePreviewChartEdbData 获取图表的指标数据
func GetThemePreviewChartEdbData(chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
	edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
	xEdbIdValue = make([]int, 0)
	yDataList = make([]data_manage.YData, 0)

	var extraConfig interface{}
	switch chartType {
	case 7: // 柱形图
		var barConfig data_manage.BarChartInfoReq
		if extraConfigStr == `` {
			errMsg = "柱方图未配置"
			err = errors.New(errMsg)
			return
		}
		err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
		if err != nil {
			errMsg = "柱方图配置异常"
			err = errors.New(errMsg)
			return
		}
		extraConfig = barConfig
	case 10: // 截面散点图
		var tmpExtraConfig data_manage.SectionScatterReq
		if extraConfigStr == `` {
			errMsg = "截面散点图未配置"
			err = errors.New(errMsg)
			return
		}
		err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
		if err != nil {
			errMsg = "截面散点配置异常"
			err = errors.New(errMsg)
			return
		}

		extraConfig = tmpExtraConfig
	case utils.CHART_TYPE_RADAR:
		var barConfig data_manage.RadarChartInfoReq
		if extraConfigStr == `` {
			errMsg = "雷达图未配置"
			err = errors.New(errMsg)
			return
		}
		err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
		if err != nil {
			errMsg = "雷达图配置异常"
			err = errors.New(errMsg)
			return
		}
		extraConfig = barConfig
	default:
		xEdbIdValue = make([]int, 0)
		yDataList = make([]data_manage.YData, 0)
	}

	// 指标对应的所有数据
	edbDataListMap, edbList, err := getThemePreviewEdbDataMapList(chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
	if err != nil {
		return
	}

	// 特殊图形数据处理
	switch chartType {
	case 7: // 柱形图
		barChartConf := extraConfig.(data_manage.BarChartInfoReq)
		xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)

		for k := range yDataList {
			yDataList[k].Unit = barChartConf.Unit
			yDataList[k].UnitEn = barChartConf.UnitEn
		}

		for _, v := range edbList {
			// 指标别名
			if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
				for _, reqEdb := range barChartConf.EdbInfoIdList {
					if v.EdbInfoId == reqEdb.EdbInfoId {
						v.EdbAliasName = reqEdb.Name
					}
				}
			}
		}
	case 10: // 截面散点图
		sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
		xEdbIdValue, dataResp, err = getThemePreviewSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)

		var tmpExtraConfig data_manage.SectionScatterReq
		if extraConfigStr == `` {
			errMsg = "截面散点图未配置"
			err = errors.New(errMsg)
			return
		}
		err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
		if err != nil {
			errMsg = "截面散点配置异常"
			err = errors.New(errMsg)
			return
		}

		// 这个数据没有必要返回给前端
		for _, v := range edbList {
			v.DataList = nil
		}

	case utils.CHART_TYPE_RADAR: //雷达图
		radarConf := extraConfig.(data_manage.RadarChartInfoReq)
		xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
	}
	return
}

func getThemePreviewEdbDataMapList(chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
	// 指标对应的所有数据
	edbDataListMap = make(map[int][]*data_manage.EdbDataList)

	for _, v := range mappingList {
		//fmt.Println("v:", v.EdbInfoId)
		item := new(data_manage.ChartEdbInfoMapping)
		item.EdbInfoId = v.EdbInfoId
		item.SourceName = v.SourceName
		item.Source = v.Source
		item.EdbCode = v.EdbCode
		item.EdbName = v.EdbName
		item.EdbNameEn = v.EdbNameEn
		item.Frequency = v.Frequency
		item.EdbType = v.EdbType
		item.FrequencyEn = GetFrequencyEn(v.Frequency)
		if v.Unit != `无` {
			item.Unit = v.Unit
		}
		item.UnitEn = v.UnitEn
		item.StartDate = v.StartDate
		item.EndDate = v.EndDate
		item.ModifyTime = v.ModifyTime
		item.EdbInfoCategoryType = v.EdbInfoCategoryType
		item.PredictChartColor = v.PredictChartColor
		item.ClassifyId = v.ClassifyId
		item.IsAxis = 1
		item.EdbInfoType = 1
		item.LeadValue = v.LeadValue
		item.LeadUnit = v.LeadUnit
		item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
		item.ChartEdbMappingId = v.ChartEdbMappingId
		item.ChartInfoId = v.ChartInfoId
		item.ChartStyle = v.ChartStyle
		item.ChartColor = v.ChartColor
		item.ChartWidth = 0
		item.IsOrder = false
		item.MaxData = v.MaxValue
		item.MinData = v.MinValue
		item.LatestValue = v.LatestValue
		item.LatestDate = v.LatestDate
		item.UniqueCode = v.UniqueCode
		item.MoveLatestDate = v.LatestDate

		var startDateReal string
		var diffSeconds int64
		if chartType == 2 { //季节性图
			startDateReal = startDate
		} else {
			if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
				var startTimeRealTemp time.Time
				startDateParse, _ := time.Parse(utils.FormatDate, startDate)
				switch v.LeadUnit {
				case "天":
					startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
				case "月":
					startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
				case "季":
					startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
				case "周":
					startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
				case "年":
					startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
				}
				if startTimeRealTemp.Before(startDateParse) {
					startDateReal = startTimeRealTemp.Format(utils.FormatDate)
					diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
				} else {
					startDateReal = startDate
					diffSeconds = 0
				}

				// 预测指标的开始日期也要偏移
				{
					day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
				}
			} else {
				startDateReal = startDate
			}
		}

		dataList := make([]*data_manage.EdbDataList, 0)
		dataList, err = chart_theme.GetChartThemeDefaultDataItemList(v.EdbInfoId, startDateReal)
		if err != nil {
			return
		}
		edbDataListMap[v.EdbInfoId] = dataList

		if diffSeconds != 0 && v.EdbInfoType == 0 {
			dataListLen := len(dataList)
			for i := 0; i < dataListLen; i++ {
				dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
			}
		}

		if chartType == 2 {
			latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
			if tmpErr != nil {
				//item.DataList = dataList
				item.IsNullData = true
				edbList = append(edbList, item)
				continue
			}

			quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
			if tErr != nil {
				err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
				return
			}
			item.DataList = quarterDataList
		} else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
			//item.DataList = dataList
		} else {
			item.DataList = dataList
		}
		edbList = append(edbList, item)
	}

	return
}

func getThemePreviewSectionScatterChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
	// 指标数据数组（10086:{"2022-12-02":100.01,"2022-12-01":102.3}）
	edbDataMap := make(map[int]map[string]float64)
	for edbInfoId, edbDataList := range edbDataListMap {
		edbDateData := make(map[string]float64)
		for _, edbData := range edbDataList {
			edbDateData[edbData.DataTime] = edbData.Value
		}
		edbDataMap[edbInfoId] = edbDateData
	}

	// edbIdList 指标展示顺序；x轴的指标顺序
	edbIdList = make([]int, 0)
	edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
	for _, v := range mappingList {
		edbIdList = append(edbIdList, v.EdbInfoId)
		edbMappingMap[v.EdbInfoId] = v
	}
	//SectionScatterSeriesInfoResp

	dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表

	for _, seriesItem := range extraConfig.SeriesList {
		var maxDate time.Time
		// 系列中的指标数据
		tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)

		var minXVal, maxXVal, minYVal, maxYVal float64
		for _, edbConf := range seriesItem.EdbInfoList {
			tmpItem := data_manage.SectionScatterEdbItemResp{
				IsShow: edbConf.IsShow,
				Name:   edbConf.Name,
				NameEn: edbConf.NameEn,
			} //单个坐标点的数据

			//X轴的数据
			{
				edbInfoId := edbConf.XEdbInfoId //X轴的指标
				edbMappingInfo, ok := edbMappingMap[edbInfoId]
				if !ok {
					continue
				}
				findDate := edbConf.XDate             //需要的日期值
				dataList := edbDataListMap[edbInfoId] //指标的所有数据值
				if len(dataList) <= 0 {
					// 没有数据的指标id
					//findDataList = append(findDataList, 0)
					continue
				}

				tmpItem.XEdbInfoId = edbInfoId
				tmpItem.XName = edbMappingInfo.EdbName
				tmpItem.XNameEn = edbMappingInfo.EdbNameEn

				switch edbConf.XDateType {
				case 1: //最新值
					findDate = dataList[len(dataList)-1].DataTime
				case 2: //近期几天
					findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)

					lenData := len(dataList) - 1
					for i := lenData; i >= 0; i-- {
						currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
						if tmpErr != nil {
							err = tmpErr
							return
						}
						if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
							findDate = dataList[i].DataTime
							break
						}
					}
				case 3: // 固定日期
					//最早的日期
					minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					//寻找固定日期的数据
					findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
						tmpDate := tmpDateTime.Format(utils.FormatDate)
						if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据，那么就返回
							findDate = tmpDate
							break
						}
					}
				default:
					err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
					return
				}
				findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
				if maxDate.IsZero() {
					maxDate = findDateTime
				} else {
					if findDateTime.After(maxDate) {
						maxDate = findDateTime
					}
				}
				if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
					tmpItem.XDate = findDate
					tmpItem.XValue = tmpValue
				} else {
					continue
				}
			}

			//Y轴的数据
			{
				edbInfoId := edbConf.YEdbInfoId //Y轴的指标
				edbMappingInfo, ok := edbMappingMap[edbInfoId]
				if !ok {
					continue
				}
				findDate := edbConf.YDate             //需要的日期值
				dataList := edbDataListMap[edbInfoId] //指标的所有数据值
				if len(dataList) <= 0 {
					// 没有数据的指标id
					//findDataList = append(findDataList, 0)
					continue
				}

				tmpItem.YEdbInfoId = edbInfoId
				tmpItem.YName = edbMappingInfo.EdbName
				tmpItem.YNameEn = edbMappingInfo.EdbNameEn

				switch edbConf.YDateType {
				case 1: //最新值
					findDate = dataList[len(dataList)-1].DataTime
				case 2: //近期几天
					findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)

					lenData := len(dataList) - 1
					for i := lenData; i >= 0; i-- {
						currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
						if tmpErr != nil {
							err = tmpErr
							return
						}
						if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
							findDate = dataList[i].DataTime
							break
						}
					}
				case 3: // 固定日期
					//最早的日期
					minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					//寻找固定日期的数据
					findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
						tmpDate := tmpDateTime.Format(utils.FormatDate)
						if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据，那么就返回
							findDate = tmpDate
							break
						}
					}
				default:
					err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
					return
				}
				findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
				if maxDate.IsZero() {
					maxDate = findDateTime
				} else {
					if findDateTime.After(maxDate) {
						maxDate = findDateTime
					}
				}
				if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
					tmpItem.YDate = findDate
					tmpItem.YValue = tmpValue
				} else {
					continue
				}
			}

			// 获取当前系列的X轴的最大最小值
			{
				if tmpItem.XValue < minXVal {
					minXVal = tmpItem.XValue
				}
				if tmpItem.XValue > maxXVal {
					maxXVal = tmpItem.XValue
				}
				if tmpItem.YValue < minYVal {
					minYVal = tmpItem.YValue
				}
				if tmpItem.YValue > maxYVal {
					maxYVal = tmpItem.YValue
				}
			}
			tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
		}

		trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
		var trendLine, rSquare string
		// 生成线性方程式
		var a, b float64
		{
			coordinateData := make([]utils.Coordinate, 0)
			for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
				tmpCoordinate1 := utils.Coordinate{
					X: tmpSeriesEdbInfo.XValue,
					Y: tmpSeriesEdbInfo.YValue,
				}
				coordinateData = append(coordinateData, tmpCoordinate1)
			}

			// 只有存在两个坐标点的时候,才能去计算线性方程和R平方
			if len(coordinateData) >= 2 {
				a, b = utils.GetLinearResult(coordinateData)
				if !math.IsNaN(a) && !math.IsNaN(b) {
					if b > 0 {
						trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
					} else {
						trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
					}

					minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
					maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
				}

				// 计算R平方
				rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
			}

			trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
				X: minXVal,
				Y: minYVal,
			}, data_manage.CoordinatePoint{
				X: maxXVal,
				Y: maxYVal,
			})
		}

		dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
			Name:            seriesItem.Name,
			NameEn:          seriesItem.NameEn,
			IsNameDefault:   seriesItem.IsNameDefault,
			Color:           seriesItem.Color,
			EdbInfoList:     tmpSeriesEdbInfoList,
			ShowTrendLine:   seriesItem.ShowTrendLine,
			ShowFitEquation: seriesItem.ShowFitEquation,
			ShowRSquare:     seriesItem.ShowRSquare,
			TrendLine:       trendLine,
			RSquare:         rSquare,
			TrendLimitData:  trendLimitData,
		})
	}

	// 截面散点图点击详情时自动更新系列名
	if len(extraConfig.SeriesList) > 0 {
		// 默认名字的时候才自动更新
		if extraConfig.SeriesList[0].IsNameDefault {
			firstXEdbInfoId := extraConfig.SeriesList[0].EdbInfoList[0].XEdbInfoId
			if v, ok := edbMappingMap[firstXEdbInfoId]; ok {
				extraConfig.SeriesList[0].Name = v.LatestDate
				extraConfig.SeriesList[0].NameEn = v.LatestDate
				dataListResp[0].Name = v.LatestDate
				dataListResp[0].NameEn = v.LatestDate
			}

		}
	}

	chartDataResp = data_manage.SectionScatterInfoResp{
		XName:       extraConfig.XName,
		XNameEn:     extraConfig.XNameEn,
		XUnitName:   extraConfig.XUnitName,
		XUnitNameEn: extraConfig.XUnitNameEn,
		YName:       extraConfig.YName,
		YNameEn:     extraConfig.YNameEn,
		YUnitName:   extraConfig.YUnitName,
		YUnitNameEn: extraConfig.YUnitNameEn,
		XMinValue:   extraConfig.XMinValue,
		XMaxValue:   extraConfig.XMaxValue,
		YMinValue:   extraConfig.YMinValue,
		YMaxValue:   extraConfig.YMaxValue,
		DataList:    dataListResp,
	}
	return
}

// GetChartThemeConfig
// @Description: 根据主题id获取主题信息，如果获取不到的话，那么就获取默认的主题
// @author: Roc
// @datetime 2023-12-19 14:31:17
// @param chartThemeId int
// @param chartType int
// @param source int
// @return chartTheme *chart_theme.ChartTheme
// @return err error
func GetChartThemeConfig(chartThemeId, source, chartType int) (chartTheme *chart_theme.ChartTheme, err error) {
	chartTheme, err = chart_theme.GetChartThemeId(chartThemeId)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}

	err = nil

	// 如果找到了，那么就返回
	if chartTheme != nil {
		return
	}

	// 没有找到的话，那么就找默认的主题

	// 查找主题类型id
	chartThemeType, err := chart_theme.GetChartThemeTypeByChartTypeAndSource(chartType, source)
	if err != nil {
		return
	}

	// 寻找默认的主题id
	chartTheme, err = chart_theme.GetChartThemeId(chartThemeType.DefaultChartThemeId)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}

	err = nil

	// 如果找到了，那么就返回
	if chartTheme != nil {
		return
	}

	// 如果还是没找到，那就系统的主题id
	chartTheme, err = chart_theme.GetSystemChartTheme(chartThemeType.ChartThemeTypeId)

	return
}
