package services

import (
	"encoding/json"
	"errors"
	"fmt"
	"hongze/hongze_ETA_mobile_api/models"
	"hongze/hongze_ETA_mobile_api/models/ppt_english"
	"hongze/hongze_ETA_mobile_api/services/alarm_msg"
	"hongze/hongze_ETA_mobile_api/utils"
	"sort"
)

const (
	ElementsTypeText  = "text"
	ElementsTypeImage = "image"
	ElementsTypeChart = "chart"
	ElementsTypeSheet = "sheet"
)

type PPTContent struct {
	//Id       int                  `json:"id" description:"此处因目录改版类型有int也有string且并没有使用到该字段所以注释掉"`
	Key      int                  `json:"key"`
	ModelId  int                  `json:"modelId"`
	Title    string               `json:"title"`
	Elements []PPTContentElements `json:"elements"`
}

type PPTContentElements struct {
	Type        string `json:"type"`
	Position    int    `json:"position"`
	Content     string `json:"content"`
	RichContent string `json:"richContent"`
	ChartId     string `json:"chartId"`
	SheetId     string `json:"sheetId"`
	SheetHeight string `json:"sheetHeight"`
	Src         string `json:"src"`
}

// PPT2内容转HTML
func pptContent2Html(content string, isEnglish bool) (htm string, err error) {
	contents := make([]PPTContent, 0)
	if e := json.Unmarshal([]byte(content), &contents); e != nil {
		err = errors.New("PPT内容转换失败")
		return
	}
	pageLen := len(contents)
	htmlContent := ``
	// iframe图表/表格域名
	chartRoot := `https://chartlib.hzinsights.com`
	if utils.RunMode == "debug" {
		chartRoot = `https://charttest.hzinsights.com`
	}
	if pageLen > 0 {
		htmlPrefix := `<p style="text-align: left; margin-top: 10px; font-size: 16px;">`
		htmlSuffix := `</p>`
		htmlBr := `<br>`
		for i := 0; i < pageLen; i++ {
			// 每页标题加粗居中
			title := contents[i].Title
			if title != "" {
				htmlContent += `<p style="font-size: 16px; text-align: left;"><strong>`
				htmlContent += title
				htmlContent += `</strong></p>`
			}
			ele := contents[i].Elements
			// 每页元素按照Position升序排序
			sort.Slice(ele, func(k, j int) bool {
				return ele[k].Position < ele[j].Position
			})
			for _, v := range ele {
				// 根据不同的Type拼接不同的内容
				htmlContent += htmlPrefix
				switch v.Type {
				case ElementsTypeText:
					htmlContent += v.RichContent
				case ElementsTypeImage:
					htmlContent += fmt.Sprint(`<img src="`, v.Src, `" class="fr-fic fr-dib fr-draggable">`)
				case ElementsTypeChart:
					if isEnglish {
						// 英文研报图表src多加一个fromPage=en, 表格暂时没有区分
						htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s&fromPage=en" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
						break
					}
					htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
				case ElementsTypeSheet:
					htmlContent += fmt.Sprintf(`<iframe src="%s/sheetshow?code=%s" class="iframe%s" width="100%%" height="%s" style="border-width:0px;"></iframe>`, chartRoot, v.SheetId, v.SheetId, v.SheetHeight)
				}
				htmlContent += htmlSuffix
			}
			// 每页中间插入一个换行符, 最后一页不插入
			currentPage := i + 1
			if currentPage != pageLen {
				htmlContent += htmlPrefix + htmlBr + htmlSuffix
			}
		}
	}
	htm = htmlContent
	return
}

// ResetPPTReport 重置PPT关联的报告(如删除关联的报告后)
func ResetPPTReport(reportId int, isEnglish bool) (err error) {
	defer func() {
		if err != nil {
			utils.FileLog.Info("%s", err.Error())
			go alarm_msg.SendAlarmMsg("重置PPT关联报告失败, ResetPPTReport Err: "+err.Error(), 3)
		}
	}()

	// 英文报告
	if isEnglish {
		en, e := ppt_english.GetPptEnglishByReportId(reportId)
		if e != nil && e.Error() != utils.ErrNoRow() {
			err = errors.New("获取英文PPT失败, Err: " + e.Error())
			return
		}
		if en != nil {
			updateCols := []string{"ReportId", "ReportCode"}
			en.ReportId = 0
			en.ReportCode = ""
			if e = en.Update(updateCols); e != nil {
				err = errors.New("更新英文PPT关联报告失败, Err: " + e.Error())
				return
			}
		}
		return
	}

	// 中文报告
	item, e := models.GetPptV2ByReportId(reportId)
	if e != nil && e.Error() != utils.ErrNoRow() {
		err = errors.New("获取PPT失败, Err: " + e.Error())
		return
	}
	if item != nil {
		updateCols := []string{"ReportId", "ReportCode"}
		item.ReportId = 0
		item.ReportCode = ""
		if e = item.Update(updateCols); e != nil {
			err = errors.New("更新PPT关联报告失败, Err: " + e.Error())
			return
		}
	}
	return
}

