package handbook

import (
	"errors"
	startMergeCfg "server/conf/startMerge"
	"server/msg"
)

type Handbook struct {
	BookList map[int]int `json:"BookList"`
}

const (
	STATUS_IDLE   = 0
	STATUS_REWARD = 1
)

func (h *Handbook) InitData() {
	if h.BookList == nil {
		h.BookList = make(map[int]int)
		InitChess := startMergeCfg.GetInitChessList()
		for _, v := range InitChess {
			h.BookList[v] = STATUS_IDLE
		}
	}
}

// 解锁图鉴
func (h *Handbook) SetHandbook(Id int) bool {
	if Id <= 0 {
		return false
	}
	_, ok := h.BookList[Id]
	if ok {
		return false
	}
	h.BookList[Id] = STATUS_IDLE
	return true
}

// 领取图鉴奖励
func (h *Handbook) GetHandbookReward(Id int) error {
	status, ok := h.BookList[Id]
	if !ok {
		return errors.New("图鉴未解锁")
	}
	if status == STATUS_REWARD {
		return errors.New("图鉴奖励已领取")
	}
	h.BookList[Id] = STATUS_REWARD
	return nil
}

func (h *Handbook) BackData() *msg.Handbook {
	var BookList []*msg.HandbookInfo
	for k, v := range h.BookList {
		BookList = append(BookList, &msg.HandbookInfo{
			ChessId: int32(k),
			Status:  int32(v),
		})
	}

	return &msg.Handbook{
		Handbooks: BookList,
	}
}
