package controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gographics/gmagick"
	setting "icloudapp.cn/tools/config"
	"icloudapp.cn/tools/entity"
	"icloudapp.cn/tools/model"
	"icloudapp.cn/tools/util/image"
	"os"
)

func Preview(ctx *gin.Context) {
	file := ctx.Query("file")
	if file == "" {
		ResponseError(ctx, entity.CodePageNotFound)
		return
	}

	path, _ := os.Getwd()

	gmagick.Initialize()
	defer gmagick.Terminate()

	thumb := image.NewImage(setting.Conf.CacheDir)
	file, err := thumb.Thumb(fmt.Sprintf("%s/%s", path, file), image.NewSize(300, 300))

	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
		return
	}

	stream, err := os.ReadFile(file)
	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
		return
	}
	ctx.Writer.WriteString(string(stream))
}

func Image(ctx *gin.Context) {
	jsonByte, err := os.ReadFile("data.json")
	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
		return
	}
	var posterJson image.PosterJson
	err = json.Unmarshal(jsonByte, &posterJson)

	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
		return
	}

	poster := image.NewPoster(ctx)

	file, err := poster.Create(posterJson)
	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("poster.Create err : %s", err.Error()), []interface{}{})
		return
	}
	if err = file.SetImageFormat("PNG"); err != nil {

		ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetImageFormat err : %s", err.Error()), []interface{}{})
		return
	}
	defer file.Destroy()
	if posterJson.Quality == 0 {
		posterJson.Quality = 50
	}
	if err = file.SetCompressionQuality(uint(posterJson.Quality)); err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
		return
	}
	//不写入文件,直接返回给浏览器
	_, err = ctx.Writer.Write(file.WriteImageBlob())
	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
		return
	}
	/*
		stream, err := os.ReadFile(file)
		if err != nil {
			fmt.Println("read error")
			ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
			return
		}

		ctx.Writer.WriteString(string(stream))
	*/
}

func Cut(ctx *gin.Context) {
	file := ctx.Query("file")
	if file == "" {
		ResponseError(ctx, entity.CodePageNotFound)
		return
	}

	path, _ := os.Getwd()

	gmagick.Initialize()
	defer gmagick.Terminate()

	thumb := image.NewImage(setting.Conf.CacheDir)
	file, err := thumb.Cut(fmt.Sprintf("%s/%s", path, file), image.NewSize(400, 400), "center")

	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
		return
	}

	stream, err := os.ReadFile(file)
	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
		return
	}

	ctx.Writer.WriteString(string(stream))
}

func SavePoster(ctx *gin.Context) {

}

func PosterInfo(ctx *gin.Context) {
	var request entity.PosterInfoRequest
	if err := ctx.ShouldBind(&request); err != nil {
		ResponseError(ctx, entity.CodeInvalidParam)
		return
	}
	sUserPoster := model.NewUserPoster(ctx)
	info, err := sUserPoster.Info(request.PosterID)

	if err != nil {
		ResponseNormal(ctx, entity.CodeServerBusy, info.Msg, nil)
		return
	}
	var api entity.UserPosterInfoForApi
	api.Code = info.Code
	api.Msg = info.Msg
	api.Body.Poster = info.Body

	ResponseNormal(ctx, api.Code, api.Msg, api.Body)
}

func DrawImage(ctx *gin.Context) {
	var request entity.PosterInfoRequest
	if err := ctx.ShouldBind(&request); err != nil {
		ResponseError(ctx, entity.CodeInvalidParam)
		return
	}
	sUserPoster := model.NewUserPoster(ctx)
	info, err := sUserPoster.Info(request.PosterID)

	if err != nil {
		ResponseNormal(ctx, entity.CodeServerBusy, info.Msg, nil)
		return
	}

	var posterJson image.PosterJson
	err = json.Unmarshal([]byte(info.Body.PosterJson), &posterJson)

	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
		return
	}

	poster := image.NewPoster(ctx)

	file, err := poster.Create(posterJson)
	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("poster.Create err : %s", err.Error()), []interface{}{})
		return
	}
	if err = file.SetImageFormat("PNG"); err != nil {

		ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetImageFormat err : %s", err.Error()), []interface{}{})
		return
	}
	defer file.Destroy()
	if posterJson.Quality == 0 {
		posterJson.Quality = 50
	}

	if err = file.SetCompressionQuality(uint(posterJson.Quality)); err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
		return
	}

	//不写入文件,直接返回给浏览器
	_, err = ctx.Writer.Write(file.WriteImageBlob())
	if err != nil {
		ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
		return
	}
}