package api

import (
	"copter-train/db/model"
	"fmt"
	"strings"

	"github.com/xuri/excelize/v2"
)

type TestExcel struct {
	Offset           int
	Row              int
	Title            string //标题
	Excel            *excelize.File
	SheetName        string
	AlignCenterStyle int
	Content          []*model.Test
	RowMap           map[string]int
	RowWidthArray    []float64
	RowsHeightArray  []map[int]float64
}

// 批量设置行高
func (b *TestExcel) setRowsHeight() {
	for _, rowHeight := range b.RowsHeightArray {
		for row, height := range rowHeight {
			b.Excel.SetRowHeight(b.SheetName, row, height)
		}
	}

}

// 获取范围内单元格的宽度 A:F
func (b *TestExcel) getRangeWidth(r string) float64 {
	rg := strings.Split(r, ":")

	if len(rg) == 1 {
		start := b.RowMap[rg[0]]
		return b.RowWidthArray[start]
	} else if len(rg) == 2 {
		start := b.RowMap[rg[0]]
		end := b.RowMap[rg[1]]
		rowr := b.RowWidthArray[start : end+1]
		width := 0.0
		for _, v := range rowr {
			width += v
		}
		return width
	}
	return 0.0
}

func (b *TestExcel) drawTitle() error {
	b.Row++
	startCell := fmt.Sprintf("A%d", b.Row)
	// marginLeft := excelize.PageMarginLeft(0.1)
	endCell := fmt.Sprintf("H%d", b.Row)

	b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7}

	b.RowWidthArray = []float64{36, 8, 20, 20, 20, 20, 8, 6}
	b.Excel.SetColWidth(b.SheetName, "A", "A", 36)
	b.Excel.SetColWidth(b.SheetName, "B", "B", 8)
	b.Excel.SetColWidth(b.SheetName, "C", "C", 20)
	b.Excel.SetColWidth(b.SheetName, "D", "D", 20)
	b.Excel.SetColWidth(b.SheetName, "E", "E", 20)
	b.Excel.SetColWidth(b.SheetName, "F", "F", 20)
	b.Excel.SetColWidth(b.SheetName, "G", "G", 8)
	b.Excel.SetColWidth(b.SheetName, "H", "H", 6)

	// b.Excel.SetPageMargins(b.SheetName, marginLeft)
	err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
	if err != nil {
		return err
	}

	style, err := b.Excel.NewStyle(&excelize.Style{
		Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
		Font:      &excelize.Font{Bold: true, Size: 18}})
	if err != nil {
		return err
	}
	err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
	if err != nil {
		return err
	}
	b.Excel.SetRowHeight(b.SheetName, b.Row, 26)

	b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
	return nil
}

func (b *TestExcel) drawTableTitle() error {
	b.Row++
	var drawCol = func(prefix string, value string) error {
		cell := fmt.Sprintf("%s%d", prefix, b.Row)
		// left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)

		// err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
		// if err != nil {
		// 	return err
		// }
		err := b.Excel.SetCellStyle(b.SheetName, cell, cell, b.AlignCenterStyle)
		if err != nil {
			return err
		}

		return b.Excel.SetCellValue(b.SheetName, cell, value)
	}

	drawCol("A", "题目")
	drawCol("B", "类型")
	drawCol("C", "选项A")
	drawCol("D", "选项B")
	drawCol("E", "选项C")
	drawCol("F", "选项D")
	drawCol("G", "分值")
	drawCol("H", "答案")
	b.Excel.SetRowHeight(b.SheetName, b.Row, 22)

	return nil
}

func (b *TestExcel) drawTableContent() error {
	b.Row++
	var DrawRow = func(rowIndex int, values ...string) float64 {
		charas := []string{"A", "B", "C", "D", "E", "F", "G", "H"}
		// 获取该行最大行高
		max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
		for i, c := range charas {
			v := ""
			if i < len(values) {
				v = values[i]
			}
			b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
			val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
			b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)

			if getRowHeight(v, b.getRangeWidth(c)) > max {
				max = getRowHeight(v, b.getRangeWidth(c))
			}
		}
		return max
	}

	tests := b.Content
	if len(tests) > 0 {
		for _, test := range tests {
			rowMaxHeight := DrawRow(b.Row, test.Content, test.Type, test.Options["A"], test.Options["B"], test.Options["C"], test.Options["D"], fmt.Sprintf("%d", *test.Socre), test.Answer)
			b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
			b.Row++
		}
	}

	return nil
}

func (b *TestExcel) Draws() {
	b.drawTitle()

	b.drawTableTitle()
	b.drawTableContent()
	// 设置行高
	b.setRowsHeight()
}

func NewTestExcel(f *excelize.File) *TestExcel {

	border := []excelize.Border{
		{Type: "top", Style: 1, Color: "000000"},
		{Type: "left", Style: 1, Color: "000000"},
		{Type: "right", Style: 1, Color: "000000"},
		{Type: "bottom", Style: 1, Color: "000000"},
	}

	styleLeft, _ := f.NewStyle(&excelize.Style{
		Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
		Border:    border,
	})

	b := &TestExcel{
		Title:            "考核试题",
		SheetName:        "Sheet1",
		Excel:            f,
		Offset:           0,
		AlignCenterStyle: styleLeft,
		RowMap:           map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7},
		RowWidthArray:    []float64{17, 17, 12, 10, 12, 12, 12, 20},
		RowsHeightArray:  make([]map[int]float64, 0),
	}

	// f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))

	return b
}

func (b *TestExcel) FormatToEmpty(str *string) {
	if *str == "0" || *str == "0.000" {
		*str = "-"
	}

}
