package model

import (
	"encoding/json"
	"strconv"
)

// 所在页面
const (
	PlayerStateOnline  = 1 // 在线
	PlayerStateOffline = 2 // 离线
	PlayerStateGameOut = 3 // 游戏外
	PlayerStateGameIn  = 4 // 游戏内
)

// 头像信息
const (
	// 符文: 位置 符文 等级 等阶
	// 头像 头像框 昵称 等级 ID, 经验
	// 主线 合作模式
	// 神话英雄： 英雄 等级
	// 宝物： 宝物 等级
	PlayerMaskHead        = 1 << 0
	PlayerMaskHeadFrame   = 1 << 1
	PlayerMaskNick        = 1 << 2
	PlayerMaskLevel       = 1 << 3
	PlayerMaskExp         = 1 << 4
	PlayerMaskMainChapter = 1 << 5
	PlayerMaskCoopChapter = 1 << 6
	PlayerMaskRune        = 1 << 7
	PlayerMaskHeros       = 1 << 8
	PlayerMaskTreasures   = 1 << 9
	PlayerMaskStatus      = 1 << 10
	PlayerMaskCredit      = 1 << 11
	PlayerMaskRenown      = 1 << 12
	PlayerMaskFort        = 1 << 13
	PlayerMaskServer      = 1 << 14
	// PlayerMaskFortSkin    = 1 << 14
	// PlayerMask = 0x

	PlayerMaskAll    = 0x7fff
	MaxPlayerMaskNum = 15
)

type RunesType []PartShow
type HerosType []*HeroShow
type TreasuresType []*TreasureShow
type FortType struct {
	Level    int64 `json:"level"` //等级
	WearSkin int64 `json:"wear"`
}
type MapKV map[int64]int64

func (r *RunesType) RedisScan(src interface{}) error {
	src2 := src.([]byte)
	err := json.Unmarshal(src2, r)
	if err != nil {
		return err
	}
	return nil
}

func (r *HerosType) RedisScan(src interface{}) error {
	src2 := src.([]byte)
	err := json.Unmarshal(src2, r)
	if err != nil {
		return err
	}
	return nil
}

func (r *TreasuresType) RedisScan(src interface{}) error {
	src2 := src.([]byte)
	err := json.Unmarshal(src2, r)
	if err != nil {
		return err
	}
	return nil
}

func (r *FortType) RedisScan(src interface{}) error {
	src2 := src.([]byte)
	err := json.Unmarshal(src2, r)
	if err != nil {
		return err
	}
	return nil
}

func (r *MapKV) RedisScan(src interface{}) error {
	src2 := src.([]byte)
	err := json.Unmarshal(src2, r)
	if err != nil {
		return err
	}
	return nil
}

type PlayerProfile struct {
	PlayerId     int64         `json:"playerId" redis:"playerId"`
	HeadId       int64         `json:"headId" redis:"headId"`
	FrameId      int64         `json:"frameId" redis:"frameId"`
	Nick         string        `json:"nick" redis:"nick"`
	Level        int64         `json:"level" redis:"level"`
	Exp          int64         `json:"exp" redis:"exp"`
	Runes        RunesType     `json:"runes" redis:"runes"`
	MainChapter  int64         `json:"mainChapter" redis:"mainChapter"`
	CoopChapter  MapKV         `json:"coopChapter" redis:"coopChapter"`
	Heros        HerosType     `json:"heros" redis:"heros"`
	Treasures    TreasuresType `json:"treasures" redis:"treasures"`
	Status       int64         `json:"status" redis:"status"` //游戏中 游戏外 离线
	LastStatusTs int64         `json:"lastStatusTs" redis:"lastStatusTs"`
	CreditScore  int64         `json:"creditScore" redis:"creditScore"` // 信誉积分
	Renown       int64         `json:"renown" redis:"renown"`           // 军衔
	Fort         *FortType     `json:"fort" redis:"fort"`
	ServerId     int64         `json:"serverId" redis:"serverId"` //几服
	Mask         uint32        `json:"-"`
}

func (pp *PlayerProfile) GetKey() int64 {
	return pp.PlayerId
}

func (p *PlayerProfile) TableName() string {
	return "playerProfile"
}

func (p *PlayerProfile) GetUniqueKey() string {
	return strconv.FormatInt(p.PlayerId, 10)
}

func NewPlayerProfile(player *GamePlayer) *PlayerProfile {
	return &PlayerProfile{
		PlayerId: player.PlayerId,
		Nick:     player.NickName,
		Level:    player.Level,
	}
}

// 必须是game模块主协程调用,并且生成的PlayerProfile的成员不能是GamePlayer的成员指针
func CreatePlayerProfile(player *GamePlayer) *PlayerProfile {
	p := new(PlayerProfile)
	p.PlayerId = player.PlayerId
	p.HeadId = player.HeadId
	p.FrameId = player.HeadFrameId
	p.Nick = player.NickName
	p.Level = player.Level
	p.Exp = player.Exp
	p.MainChapter = 0 // 默认值，可以根据实际情况调整
	if player.Chapters != nil {
		cp := findChapterProcess(player.Chapters.Process, LevelTypeMainSingle)
		if cp != nil {
			levelId := CombineLevelId(LevelTypeMainSingle, cp.Chapter, cp.Level)
			p.MainChapter = levelId
		}

		p.CoopChapter = make(MapKV) // 默认值，可以根据实际情况调整
		cp = findChapterProcess(player.Chapters.Process, LevelTypeCooperation)
		if cp != nil {
			levelId := CombineLevelId(LevelTypeCooperation, cp.Chapter, cp.Level)
			p.CoopChapter[levelId] = cp.Wave
		}
	}

	p.Runes = player.GetRuneShow()
	p.Heros = player.GetHeroShow()
	p.Treasures = player.GetTreasureShow()

	if player.IsOnline() {
		p.Status = PlayerStateOnline
		p.LastStatusTs = player.LastLoginTick
	} else {
		p.Status = PlayerStateOffline
		p.LastStatusTs = player.LastLogoutTick
	}

	p.CreditScore = player.MapValues[MapValueCreditScore]
	p.Renown = player.MapValues[MapValueRenown]

	p.Fort = new(FortType)
	if player.Fort != nil {
		p.Fort.Level = player.Fort.Level
		p.Fort.WearSkin = player.Fort.WearSkin
	}
	p.ServerId = player.LineId

	return p
}

// 获取关卡进度
func findChapterProcess(process []*ChapterProcess, typ int64) *ChapterProcess {
	for _, v := range process {
		if v.Type == typ {
			return v
		}
	}
	return nil
}

type PlayerBrief struct {
	PlayerId         int64  `json:"playerId"`
	HeadId           int64  `json:"headId"`           //头像
	FrameId          int64  `json:"frameId"`          //头像框
	Nick             string `json:"nick"`             //昵称
	Level            int64  `json:"level"`            //等级
	ServerId         int64  `json:"serverId"`         //服务器id
	Online           int64  `json:"online"`           //在线状态
	LastLogInOutTick int64  `json:"lastLogInOutTick"` //上次登录时间
	// FriendStatus  int64  `json:"friendStatus"`  // 0未添加 1已申请未通过 2已是好友
}

func CreatePlayerBrief(player *GamePlayer) *PlayerBrief {
	p := new(PlayerBrief)
	p.PlayerId = player.PlayerId
	p.HeadId = player.HeadId
	p.FrameId = player.HeadFrameId
	p.Nick = player.NickName
	p.Level = player.Level
	p.ServerId = player.LineId
	if player.IsOnline() {
		p.Online = PlayerStateOnline
		p.LastLogInOutTick = player.LastLoginTick
	} else {
		p.Online = PlayerStateOffline
		p.LastLogInOutTick = player.LastLogoutTick
	}

	return p
}
