package main

import (
	"time"

	"mongo.games.com/goserver/core/module"

	"mongo.games.com/game/common"
	hall_proto "mongo.games.com/game/protocol/gamehall"
	"mongo.games.com/game/protocol/pets"
	"mongo.games.com/game/srvdata"
)

const (
	ItemObtain  = iota //得到
	ItemConsume        //消耗
)

var PetMgrSington = &PetMgr{
	//RPInfos: make(map[int32]*RoleAndPetInfo),
}

type PetMgr struct {
	//RPInfos map[int32]*RoleAndPetInfo
}
type RoleAndPetInfo struct {
	ModId      int32  //模型id
	Name       string //名字
	Story      string //人物背景介绍
	AwardTitle string //奖励标题
	MaxLevel   int32  //最大等级
}

func (this *PetMgr) ModuleName() string {
	return "PetMgr"
}

// 人物
func (this *PetMgr) GetRoleInfos(p *Player) []*pets.RoleInfo {
	p.InitRolesAndPets()

	rolesInfo := srvdata.RolePetMgrSington.Roles
	if rolesInfo != nil {
		var newRoles = make([]*pets.RoleInfo, 0)
		for roleId, roles := range rolesInfo {
			level := p.Roles.ModUnlock[roleId]
			if len(roles) >= int(level) {
				rol := roles[level]
				role := &pets.RoleInfo{
					Id:        rol.Id,
					RoleId:    rol.RoleId,
					Name:      rol.Name,
					Grade:     rol.Grade,
					Level:     rol.Level,
					Fragment:  rol.Fragment,
					Amount:    rol.Amount,
					AwardType: rol.AwardType,
					Award:     rol.Award,
					AwardRate: rol.AwardRate,
				}
				if level > 0 {
					role.IsUnlock = true
					if p.Roles.ModId == roleId {
						role.IsUsing = true
					}
				}

				if level == 0 {
					role.NextAward = roles[1].Award
				} else {
					if int32(len(roles)) > level+1 {
						role.NextAward = roles[level+1].Award
					}
				}

				roleOther := this.GetIntroductionByModId(role.RoleId)
				role.MaxLevel = roleOther.MaxLevel
				role.Name = roleOther.Name
				role.Story = roleOther.Story
				role.AwardTitle = roleOther.AwardTitle
				///
				item := BagMgrSingleton.GetItem(p.SnId, role.Fragment)
				if item != nil {
					role.HaveAmount = int32(item.ItemNum)
				}
				newRoles = append(newRoles, role)
			}
		}
		return newRoles
	}
	return nil
}
func (this *PetMgr) GetRoleInfo(p *Player, modId int32) *pets.RoleInfo {
	p.InitRolesAndPets()

	level := p.Roles.ModUnlock[modId]
	roleInfo := srvdata.RolePetMgrSington.GetRoleByRoleIdAndLevel(modId, level)
	if roleInfo != nil {
		role := &pets.RoleInfo{
			Id:        roleInfo.Id,
			RoleId:    roleInfo.RoleId,
			Name:      roleInfo.Name,
			Grade:     roleInfo.Grade,
			Level:     roleInfo.Level,
			Fragment:  roleInfo.Fragment,
			Amount:    roleInfo.Amount,
			AwardType: roleInfo.AwardType,
			Award:     roleInfo.Award,
			AwardRate: roleInfo.AwardRate,
		}
		if level > 0 {
			role.IsUnlock = true
			if p.Roles.ModId == roleInfo.RoleId {
				role.IsUsing = true
			}
		}
		nextInfo := srvdata.RolePetMgrSington.GetRoleByRoleIdAndLevel(modId, level+1)
		if nextInfo != nil {
			role.NextAward = nextInfo.Award
		}
		roleOther := this.GetIntroductionByModId(modId)
		role.MaxLevel = roleOther.MaxLevel
		role.Name = roleOther.Name
		role.Story = roleOther.Story
		role.AwardTitle = roleOther.AwardTitle
		//
		item := BagMgrSingleton.GetItem(p.SnId, role.Fragment)
		if item != nil {
			role.HaveAmount = int32(item.ItemNum)
		}
		return role
	}
	return nil
}

// ////////////////////////////////////////////////////////////////////////
// 宠物
func (this *PetMgr) GetPetInfos(p *Player) []*pets.PetInfo {
	p.InitRolesAndPets()

	petsInfo := srvdata.RolePetMgrSington.Pets
	if petsInfo != nil {
		var newPets = make([]*pets.PetInfo, 0)
		for petId, petInfo := range petsInfo {
			level := p.Pets.ModUnlock[petId]
			if len(petInfo) >= int(level) {
				pet := petInfo[level]
				newPet := &pets.PetInfo{
					Id:        pet.Id,
					PetId:     pet.PetId,
					Name:      pet.Name,
					Grade:     pet.Grade,
					Level:     pet.Level,
					Fragment:  pet.Fragment,
					Amount:    pet.Amount,
					AwardType: pet.AwardType,
					Award:     pet.Award,
					AwardRate: pet.AwardRate,
				}
				if level > 0 {
					newPet.IsUnlock = true
					if p.Pets.ModId == petId {
						newPet.IsUsing = true
					}
				}

				if level == 0 {
					newPet.NextAward = petInfo[1].Award
				} else {
					if int32(len(petInfo)) > level+1 {
						newPet.NextAward = petInfo[level+1].Award
					}
				}

				petOther := this.GetIntroductionByModId(newPet.PetId)
				newPet.MaxLevel = petOther.MaxLevel
				newPet.Name = petOther.Name
				newPet.Story = petOther.Story
				newPet.AwardTitle = petOther.AwardTitle
				///
				item := BagMgrSingleton.GetItem(p.SnId, newPet.Fragment)
				if item != nil {
					newPet.HaveAmount = int32(item.ItemNum)
				}
				newPets = append(newPets, newPet)
			}
		}
		return newPets
	}
	return nil
}
func (this *PetMgr) GetPetInfo(p *Player, modId int32) *pets.PetInfo {
	p.InitRolesAndPets()

	level := p.Pets.ModUnlock[modId]
	petInfo := srvdata.RolePetMgrSington.GetPetByPetIdAndLevel(modId, level)
	if petInfo != nil {
		newPet := &pets.PetInfo{
			Id:        petInfo.Id,
			PetId:     petInfo.PetId,
			Name:      petInfo.Name,
			Grade:     petInfo.Grade,
			Level:     petInfo.Level,
			Fragment:  petInfo.Fragment,
			Amount:    petInfo.Amount,
			AwardType: petInfo.AwardType,
			Award:     petInfo.Award,
			AwardRate: petInfo.AwardRate,
		}
		if level > 0 {
			newPet.IsUnlock = true
			if p.Roles.ModId == petInfo.PetId {
				newPet.IsUsing = true
			}
		}
		nextInfo := srvdata.RolePetMgrSington.GetPetByPetIdAndLevel(modId, level+1)
		if nextInfo != nil {
			newPet.NextAward = nextInfo.Award
		}
		petOther := this.GetIntroductionByModId(modId)
		newPet.MaxLevel = petOther.MaxLevel
		newPet.Name = petOther.Name
		newPet.Story = petOther.Story
		newPet.AwardTitle = petOther.AwardTitle
		//
		item := BagMgrSingleton.GetItem(p.SnId, newPet.Fragment)
		if item != nil {
			newPet.HaveAmount = int32(item.ItemNum)
		}
		return newPet
	}
	return nil
}

// 商品人物总加成  人物功能变动需要修改
func (this *PetMgr) GetShopAward(shopInfo *ShopInfo, p *Player) (award, roleId int32) {
	if shopInfo.Ad > 0 && shopInfo.Type == ShopTypeCoin {
		id, add := srvdata.RolePetMgrSington.GetRoleAdd(p.PlayerData, common.RoleAddADV)
		award += add
		roleId = id
	}

	if shopInfo.Ad == 0 && shopInfo.Type == ShopTypeCoin {
		id, add := srvdata.RolePetMgrSington.GetRoleAdd(p.PlayerData, common.RoleAddCoin)
		award += add
		roleId = id
	}

	//VIP商城没有人物加成
	if shopInfo.Page == ShopPageVip || shopInfo.Page == ShopPagePhoneScore || shopInfo.Page == ShopPagePhoneScore_google {
		award = 0
	}
	return award, roleId
}

// 宠物加成  宠物功能变动需要修改
func (this *PetMgr) GetAwardPetByWelf(p *Player) (award int64) {
	petChick := this.GetPetInfo(p, 1000001)
	if petChick != nil && petChick.Level > 0 {
		award = int64(petChick.Award)
	}
	return
}
func (this *PetMgr) GetIntroductionByModId(modId int32) *RoleAndPetInfo {
	mod := srvdata.PBDB_Game_IntroductionMgr.GetData(modId)
	if mod == nil {
		return &RoleAndPetInfo{}
	}
	return &RoleAndPetInfo{
		ModId:      mod.Id,
		Name:       mod.Name,
		Story:      mod.Story,
		AwardTitle: mod.AwardTitle,
		MaxLevel:   mod.LevelMax,
	}
}
func (this *PetMgr) CheckShowRed(p *Player) {
	if p == nil {
		return
	}
	var roleRed, petRed bool
	if p.Roles != nil {
		needAmount := make(map[int32]int32)
		for roleId, rol := range srvdata.RolePetMgrSington.Roles {
			if level, ok := p.Roles.ModUnlock[roleId]; ok {
				needAmount[rol[level].Fragment] = rol[level].Amount
			} else {
				needAmount[rol[level].Fragment] = rol[level].Amount
			}
		}
		for fragment, amount := range needAmount {
			item := BagMgrSingleton.GetItem(p.SnId, fragment)
			if item != nil && item.ItemNum >= int64(amount) && amount != 0 {
				roleRed = true
				break
			}
		}
	}
	if p.Pets != nil {
		needAmount := make(map[int32]int32)
		for petId, pet := range srvdata.RolePetMgrSington.Pets {
			if level, ok := p.Pets.ModUnlock[petId]; ok {
				needAmount[pet[level].Fragment] = pet[level].Amount
			} else {
				needAmount[pet[level].Fragment] = pet[level].Amount
			}
		}
		for fragment, amount := range needAmount {
			item := BagMgrSingleton.GetItem(p.SnId, fragment)
			if item != nil && item.ItemNum >= int64(amount) && amount != 0 {
				petRed = true
				break
			}
		}
	}
	if roleRed {
		p.SendShowRed(hall_proto.ShowRedCode_Role, 0, 1)
	}
	if petRed {
		p.SendShowRed(hall_proto.ShowRedCode_Pet, 0, 1)
	}
}

//////////////////////////////////

func (this *PetMgr) Init() {

}
func (this *PetMgr) Update() {

}

func (this *PetMgr) Shutdown() {
	module.UnregisteModule(this)
}

func init() {
	module.RegisteModule(PetMgrSington, time.Second, 0)
}
