package gm_services

import (
	"errors"
	"gadmin/internal/admin/data"
	"gadmin/internal/admin/forms"
	"github.com/jinzhu/copier"
)

var (
	stopType          = "stopServerNotice"
	stopText          = "服务器将于%s停服维护，为各位玩家带来的不便，敬请谅解。"
	errNoticeNotFound = errors.New("广播记录已被删除或不存在")
)

func GetNoticeList(params forms.NoticeListReq) (notices []forms.NoticeItem, err error) {
	// var stamps []api.NoticeItem
	for _, v := range data.Db {
		var item forms.NoticeItem
		copier.Copy(&item, &v)
		notices = append(notices, item)
	}
	if notices == nil {
		notices = make([]forms.NoticeItem, 0)
	}

	return
}

func AddNotice(params forms.AddNoticeReq) (notice *data.Notice, err error) {
	if params.Type == "" {
		params.Type = stopType
	}
	notice = data.AddNotice(params)
	// if params.Content != "" {
	//
	// 	if err == nil {
	// 		rpc_share.MsgMap[msgItem.MsgId] = fmt.Sprintf("%s,GM发布了广播任务 %v",
	// 			utils.FormatSecond(time.Now()), msgItem)
	// 	}
	// } else {
	// 	err = errors.New("广播内容不能为空")
	// }

	return
}

func DelNotice(id string) (err error) {
	if _, ok := data.Db[id]; ok {
		delete(data.Db, id)
	} else {
		err = errNoticeNotFound
	}

	return
}
