package logic

import (
	"errors"
	"fmt"
	"fonchain-fiee/pkg/model"
	"fonchain-fiee/pkg/service/bundle/common"
	"fonchain-fiee/pkg/service/upload"
	"github.com/signintech/gopdf"
	"go.uber.org/zap"
	"log"
	"os"
	"strconv"
	"time"
)

func SignContractV2(customerNum, contract string, price float32, expirationDate string) (outputUrl string, err error) {
	filePath := model.MediaPath + customerNum + time.Now().Format("20060102150405") + ".pdf"
	downloadFileErr := DownloadFile(filePath, contract)
	if downloadFileErr != nil {
		zap.L().Error("download file error: ", zap.Error(downloadFileErr))
		//service.Error(c, errors.New(common.ErrorDownloadFile))
		return outputUrl, errors.New(common.ErrorDownloadFile)
	}

	signFile := model.MediaPath + customerNum + "signed" + time.Now().Format("20060102150405") + ".pdf"

	/*signErr := InsertSignature(filePath, signFile, signImgPath, contractNo, idNo, telNum, address, price)
	if signErr != nil {
		zap.L().Error("insert signature error: ", zap.Error(signErr))
		return outputUrl, errors.New(common.ErrorInsertSignature)
	}*/

	signErr := InsertSignatureV2(filePath, signFile, price, expirationDate)
	if signErr != nil {
		zap.L().Error("insert signature error: ", zap.Error(signErr))
		return outputUrl, errors.New(common.ErrorInsertSignature)
	}

	os.Remove(filePath)

	outputUrl, ossErr := upload.PutBos(signFile, upload.PdfType, true)
	if ossErr != nil {
		return "", errors.New(common.ErrorUploadFile)
	}
	return outputUrl, nil
}

func InsertSignatureV2(templatePath, outputPath string, price float32, expirationDate string) error {
	pdf := gopdf.GoPdf{}
	pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})

	// 导入模板文件中的页面
	err := pdf.ImportPagesFromSource(templatePath, "/MediaBox")
	if err != nil {
		log.Fatalf("无法导入页面: %v", err)
	}

	// 获取模板文件的总页数
	totalPages := pdf.GetNumberOfPages()
	fmt.Printf("模板文件的总页数: %d\n", totalPages)

	pricePage := 3

	limitTimePage := 4

	tffErr := pdf.AddTTFFont("simfang", "./data/simfang.ttf")
	if tffErr != nil {
		fmt.Printf("加载中文字体失败: %v\n", tffErr)
		log.Fatalf("加载中文字体失败: %v", tffErr)
	}

	// 设置字体和字号
	err = pdf.SetFont("simfang", "", 14)
	if err != nil {
		fmt.Printf("设置字体失败: %v\n", err)
		log.Fatalf("设置字体失败: %v", err)
	}

	// 填 金额
	pdf.SetPage(pricePage)
	pdf.SetX(383)
	pdf.SetY(351)
	pdf.Cell(nil, strconv.FormatFloat(float64(price), 'f', 2, 64))

	pdf.SetX(345)
	pdf.SetY(383)
	pdf.Cell(nil, strconv.FormatFloat(float64(price), 'f', 2, 64))
	// 写 有效期
	pdf.SetPage(limitTimePage)

	//  英文格式的时间
	parsedTime, err := time.Parse("2006-01-02", expirationDate)
	if err != nil {
		panic("日期格式错误!")
	}
	year := parsedTime.Format("2006") // "2006"
	month := parsedTime.Format("01")  // "01"
	day := parsedTime.Format("02")    // "02"
	pdf.SetX(160)
	pdf.SetY(387)
	pdf.Cell(nil, expirationDate)

	pdf.SetX(330)
	pdf.SetY(403)
	pdf.Cell(nil, year)

	pdf.SetX(396)
	pdf.SetY(403)
	pdf.Cell(nil, month)

	pdf.SetX(443)
	pdf.SetY(403)
	pdf.Cell(nil, day)

	// 生成新的 PDF
	if err = pdf.WritePdf(outputPath); err != nil {
		//zap.L().Error("WritePdf err", zap.Error(err))
		return errors.New("error writing final PDF")
	}

	return nil
}