package cast

import (
	"fmt"
	"fonchain-fiee/api/cast"
	modelCast "fonchain-fiee/pkg/model/cast"
	"strings"

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

type Work struct {
}

func (w *Work) ExportExcelWorkList(data []*cast.WorkListResp_Info) (*excelize.File, error) {
	f := excelize.NewFile()
	sheetName := "Sheet1"
	f.SetSheetName("Sheet1", sheetName)

	// 使用 StreamWriter
	sw, err := f.NewStreamWriter(sheetName)
	if err != nil {
		return nil, err
	}

	// 表头
	headers := []interface{}{
		"艺人", "手机号", "用户编号", "作品标题", "脚本", "作品类型", "类型", "Ins", "Tiktok", "DM", "Youtube", "Bulesky", "作品状态", "验收确认类型",
		"说明", "发布账号", "管理人", "上传时间", "待艺人确认时间", "验收确认通过时间", "发布成功时间",
	}
	if err := sw.SetRow("A1", headers); err != nil {
		return nil, err
	}

	// 内容
	rowIndex := 2
	for _, info := range data {
		// 拼接字段
		//platformNames := strings.Join(func() []string {
		//	arr := make([]string, 0, len(info.PlatformIDs))
		//	for _, v := range info.PlatformIDs {
		//		arr = append(arr, modelCast.PlatformIDMM[int(v)])
		//	}
		//	return arr
		//}(), "/")

		var mediaAccountNames string
		var managerNames string
		//判断是否为空
		if len(info.MediaAccountNames) > 0 {
			mediaAccountNames = strings.Join(info.MediaAccountNames, "/")
		} else {
			mediaAccountNames = ""
		}
		if len(info.ManagerUserNames) > 0 {
			managerNames = strings.Join(info.ManagerUserNames, "/")
		} else {
			managerNames = ""
		}

		if managerNames == "//" {
			managerNames = ""
		}

		row := []interface{}{
			info.ArtistName,
			info.ArtistPhone,
			info.ArtistSubNum,
			info.Title,
			info.ScriptTitle,
			modelCast.WorkCategoryMM[int(info.WorkCategory)],
			modelCast.WorkCostTypeMM[int(info.CostType)],
			modelCast.PlatformPublishStatusMM[cast.PublishStatusENUM(info.InsStatus)],
			modelCast.PlatformPublishStatusMM[cast.PublishStatusENUM(info.TiktokStatus)],
			modelCast.PlatformPublishStatusMM[cast.PublishStatusENUM(info.DmStatus)],
			modelCast.PlatformPublishStatusMM[cast.PublishStatusENUM(info.YoutubeStatus)],
			modelCast.PlatformPublishStatusMM[cast.PublishStatusENUM(info.BlueskyStatus)],
			modelCast.WorkStatusMM[int(info.WorkStatus)],
			modelCast.ConfirmTypeMM[int(info.ConfirmType)],
			info.Remark,
			mediaAccountNames,
			managerNames,
			info.SubmitTime,
			info.Status4Time,
			info.Status9Time,
			info.Status6Time,
			//platformNames,
			//info.SubmitTime,
		}

		cell, _ := excelize.CoordinatesToCellName(1, rowIndex)
		if err := sw.SetRow(cell, row); err != nil {
			return nil, err
		}

		rowIndex++
	}

	// 结束流写入
	if err := sw.Flush(); err != nil {
		return nil, err
	}

	return f, nil
}

func (w *Work) ExportPublishLogList(data []*cast.PublishLogInfo, savePath string) error {
	f := excelize.NewFile()
	defer f.Close()

	sheetName := "Sheet1"
	f.SetSheetName("Sheet1", sheetName)

	headers := []string{
		"状态", "失败原因", "提交发布时间", "发布平台", "发布方式", "作品类型", "作品标题", "艺人", "用户编号",
	}
	for col, h := range headers {
		cell, _ := excelize.CoordinatesToCellName(col+1, 1)
		f.SetCellValue(sheetName, cell, h)
	}

	for rowIndex, info := range data {
		row := rowIndex + 2
		platformName := modelCast.PlatformNameKv[info.PlatformID]
		if platformName == "" {
			platformName = fmt.Sprintf("%d", info.PlatformID)
		}
		values := []interface{}{
			modelCast.PublishStatusMM[int(info.PublishMediaStatus)],
			info.Detail,
			info.CreatedAt,
			modelCast.PlatformNameKv[info.PlatformID],
			modelCast.PublishSourceMM[int(info.PublishSource)],
			modelCast.WorkCategoryMM[int(info.WorkCategory)],
			info.Title,
			info.ArtistName,
			info.ArtistSubNum,
		}
		for col, v := range values {
			cell, _ := excelize.CoordinatesToCellName(col+1, row)
			f.SetCellValue(sheetName, cell, v)
		}
	}

	return f.SaveAs(savePath)
}
