package friend

import (
	"fmt"
	"server/GoUtil"
	cardCfg "server/conf/card"
	"server/game/mod/card"
	"server/game/mod/item"
)

type FriendMod struct {
	FriendList map[int]struct{}          // 好友列表
	ApplyList  map[int]int64             // 请求列表
	SendApply  map[int]int64             // 发送的申请
	Card       map[string]*card.CardInfo // 收到的申请交换
	Log        []*LogInfo                // 日志
	AutoId     int
	Id         int64 // 已同步msg ID
}

const (
	LOG_TYPE_FRIEND_APPLY      = 1  // 收到申请好友
	LOG_TYPE_FRIEND_BECOME     = 2  // 成为好友
	LOG_TYPE_CARD_EX_SEND      = 3  // 发起卡牌交换申请
	LOG_TYPE_CARD_SEND         = 4  // 赠送卡牌
	LOG_TYPE_CARD_GIVE         = 5  // 请求卡牌
	LOG_TYPE_CARD_SELECT_GET   = 6  // 选择卡牌交换
	LOG_TYPE_CARD_ACCEPT_GIVE  = 7  // 接受卡牌
	LOG_TYPE_CARD_EX_GET       = 8  // 收到卡牌交换申请
	LOG_TYPE_CARD_SELECT_SEND  = 9  // 选择卡牌交换
	LOG_TYPE_CARD_EX_SUCCESS_1 = 10 // 卡牌交换成功
	LOG_TYPE_CARD_EX_SUCCESS_2 = 11 // 卡牌交换成功
	LOG_TYPE_FRIEND_DELETE     = 14 // 删除好友
	LOG_TYPE_PLAYROOM_VISIT    = 15 // 拜访玩家
	LOG_TYPE_HANDBOOK          = 16 // 图鉴收集
)

const (
	FRIEND_NOTIFY_APPLY  = 1
	FRIEND_NOTIFY_AGREE  = 2
	FRIEND_NOTIFY_REFUSE = 3
	FRIEND_NOTIFY_DEL    = 4
)

type LogInfo struct {
	Id     int
	Uid    int
	Type   int
	Time   int64
	Param  string
	Upvote bool // 点赞
}

func (f *FriendMod) InitData() {
	if f.FriendList == nil {
		f.FriendList = make(map[int]struct{})
	}
	if f.ApplyList == nil {
		f.ApplyList = make(map[int]int64)
	}
	if f.Card == nil {
		f.Card = make(map[string]*card.CardInfo)
	}
	if f.SendApply == nil {
		f.SendApply = make(map[int]int64)
	}
}

func (f *FriendMod) GetSyncId() int64 {
	return f.Id
}

func (f *FriendMod) SetSyncId(Id int64) {
	f.Id = Id
}

func (f *FriendMod) AddFriend(id int) {
	f.FriendList[id] = struct{}{}
	delete(f.ApplyList, id)
}

func (f *FriendMod) GetCardInfo(Id string) *card.CardInfo {
	v, ok := f.Card[Id]
	if !ok {
		return nil
	}
	return v
}

func (f *FriendMod) SetCardInfo(CardInfo *card.CardInfo) {
	f.Card[CardInfo.Id] = CardInfo
}

func (f *FriendMod) DelCardInfo(Id string) {
	delete(f.Card, Id)
}

func (f *FriendMod) DelFriend(id int) {
	delete(f.FriendList, id)
}

func (f *FriendMod) CheckFriend(Uid int) bool {
	_, ok := f.FriendList[Uid]
	return ok
}

func (f *FriendMod) RefuseApply(id int) {
	delete(f.ApplyList, id)
}

func (f *FriendMod) CheckApply(id int) bool {
	_, ok := f.ApplyList[id]
	return ok
}
func (f *FriendMod) GetFriendNum() int {
	return len(f.FriendList)
}
func (f *FriendMod) GetFriendList() []int {
	var list []int
	for k := range f.FriendList {
		list = append(list, k)
	}
	return list
}

// 收到申请
func (f *FriendMod) AddFriendApply(Uid int) {
	f.ApplyList[Uid] = GoUtil.Now()
}

// 发送申请
func (f *FriendMod) AddSendApply(Uid int) bool {
	if _, ok := f.SendApply[Uid]; ok {
		return true
	}
	f.SendApply[Uid] = GoUtil.Now()
	return false
}

func (f *FriendMod) CheckSendApply(Id int) bool {
	_, ok := f.SendApply[Id]
	return ok
}

func (f *FriendMod) AgreeApply(UId int) {
	f.AddFriend(UId)
	delete(f.SendApply, UId)
	delete(f.ApplyList, UId)
}

func (f *FriendMod) AddLog(Uid, Type int, Param string) int {
	f.AutoId++
	f.Log = append(f.Log, &LogInfo{
		Id:    f.AutoId,
		Uid:   Uid,
		Type:  Type,
		Time:  GoUtil.Now(),
		Param: Param,
	})
	return f.AutoId
}

func (f *FriendMod) ResetGoldCardEx() {
	for _, v := range f.Card {
		if cardCfg.CheckCardIsGold(v.CardId) || cardCfg.CheckCardIsGold(v.ExId) {
			v.EndTime = 1
		}
	}
}

func (f *FriendMod) Upvote(Id int) ([]*item.Item, error) {
	info := f.Log[Id]
	if info == nil {
		return nil, fmt.Errorf("log not exist")
	}
	if info.Upvote {
		return nil, fmt.Errorf("already upvote")
	}
	info.Upvote = true
	return []*item.Item{item.NewItem(item.ITEM_ENERGY_ID, 1)}, nil
}
