package advertise

import (
	"Wine-Server/utils"
	"Wine-Server/utils/tables"
	"errors"
	"github.com/gorilla/websocket"
	"os"
	"strings"
)

func Query(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsError("auth failed."))
		return
	}
	var param queryParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		_ = conn.WriteJSON(utils.WsError("param error"))
		return
	}
	total, ads, err := tables.AdvertisesQuery(param.Desc, param.Type, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsError("query advertises failed"))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superConfigAdvertiseQueryRes", utils.JsonType{"total": total, "ads": ads}))
}

func removeAdvFile(src string) error {
	index := strings.Index(src, "static/adv")
	if index == -1 {
		return errors.New("error src")
	}
	return os.Remove(src[index:])
}

func Delete(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsError("auth failed."))
		return
	}
	var ids []uint32
	err := utils.AnyTrans(data, &ids)
	if err != nil {
		_ = conn.WriteJSON(utils.WsError("param error"))
		return
	}
	Err := false
	for _, aid := range ids {
		adv := tables.AdvertiseTable{Id: aid}
		err = adv.Get()
		if err != nil {
			utils.Logger.Println(err)
			Err = true
		} else {
			err = removeAdvFile(adv.Src)
			if err != nil {
				utils.Logger.Println(err)
				Err = true
			} else {
				_ = adv.Delete()
			}
		}
	}
	if Err {
		_ = conn.WriteJSON(utils.WsEvent("superConfigAdvertiseDeleteRes", utils.Fail("删除失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superConfigAdvertiseDeleteRes", utils.Success(nil)))
}

// Add TODO: below
func Add(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsError("auth failed."))
		return
	}
	var param addParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		_ = conn.WriteJSON(utils.WsError("param error"))
		return
	}
	if param.Image == "" {
		_ = conn.WriteJSON(utils.WsEvent("superConfigWineAddRes", utils.Fail("图片数据不可为空")))
		return
	}
	var id uint16
	id, err = tables.WineQueryMaxId()
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("superConfigWineAddRes", utils.Fail("添加失败 01")))
		return
	}
	var reason string
	reason, err = utils.SaveBase64(param.Image, utils.Format("static/images/wine/%d.png", id))
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("superConfigWineAddRes", utils.Fail(reason)))
		return
	}
	wine := tables.WineTable{
		Id: id + 1, Name: param.Name, Price: param.Price, Degree: param.Degree,
		Density: param.Density, Describe: param.Describe,
		Picture: utils.Format("%s/static/images/wine/%d.png", utils.ServerPrefix, id),
	}
	err = wine.Insert()
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("superConfigWineAddRes", utils.Fail("添加失败 02")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superConfigWineAddRes", utils.Success(nil)))
}

func Update(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsError("auth failed."))
		return
	}
	var param updateParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		_ = conn.WriteJSON(utils.WsError("param error"))
		return
	}
	wine := tables.WineTable{Id: param.Id}
	err = wine.Get()
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("superConfigWineUpdateRes", utils.Fail("no such wine")))
		return
	}
	if param.Image != "" {
		var reason string
		reason, err = utils.SaveBase64(param.Image, utils.Format("static/images/wine/%d.png", param.Id))
		if err != nil {
			_ = conn.WriteJSON(utils.WsEvent("superConfigWineUpdateRes", utils.Fail(reason)))
			return
		}
		wine.Picture = utils.Format("%s/static/images/wine/%d.png", utils.ServerPrefix, param.Id)
	}
	wine.Name, wine.Price, wine.Degree = param.Name, param.Price, param.Degree
	wine.Density, wine.Describe = param.Density, param.Describe
	err = wine.UpdateSelf()
	if err != nil {
		_ = conn.WriteJSON(utils.WsEvent("superConfigWineUpdateRes", utils.Fail("更新失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superConfigWineUpdateRes", utils.Success(nil)))
}
