package controller

import (
	"ktogame/models"
)

func (uc *UserController) Reinput(user string) {
	var ui models.UserInfo
	ok, err := uc.engine.Id(user).Get(&ui)
	if err != nil {
		ErrResponse(uc.Controller, err)
		return
	}
	if !ok {
		ErrResponse(uc.Controller, "db get failed!")
		return
	}
	if ui.AvailableReinput < ModBase*Decimals {
		ErrResponse(uc.Controller, "no available reinput balance")
		return
	}

	///update availableReinput by left in db
	///update totalReinput
	left := uint64(ui.AvailableReinput) % (ModBase * Decimals)
	reinputValue := ui.AvailableReinput - float64(left)
	ui.AvailableReinput = float64(left)
	ui.TotalReinputed += reinputValue
	if ui.TotalReinputed >= CommunityLimit*Decimals {
		ui.State = CommunityUsers
	}
	_, err = uc.engine.ID(user).Cols("available_reinput").Update(&ui)
	if err != nil {
		ErrResponse(uc.Controller, err)
		return
	}
	_, err = uc.engine.ID(user).Cols("total_reinput").Update(&ui)
	if err != nil {
		ErrResponse(uc.Controller, err)
		return
	}

	///update Recommendation Rewards
	///update direct recommend rewards
	rewards := reinputValue * 10 / 100
	if ui.Direct != -1 {
		err := UpdateAvailableRewards(uc.engine, rewards, ui.Direct)
		if err != nil {
			ErrResponse(uc.Controller, err)
			return
		}
	}
	///update indirect recommend rewards
	if ui.Indirect != -1 {
		err := UpdateAvailableRewards(uc.engine, rewards, ui.Indirect)
		if err != nil {
			ErrResponse(uc.Controller, err)
			return
		}
	}
	err = UpdateRewardsPool(uc.engine, reinputValue)
	if err != nil {
		ErrResponse(uc.Controller, err)
		return
	}
	err = UpdateSuperiorsPerformance(uc.engine, ui.Superiors, reinputValue)
	if err != nil {
		ErrResponse(uc.Controller, err)
		return
	}
	TxObjectResponse(uc.Controller, "success")
}
func (uc *UserController) GetSignature(user string) {
	var uinfo models.UserInfo
	uc.engine.Id(user).Get(&uinfo)

}
func (uc *UserController) GetUserInfo(amount uint64) {}
func (uc *UserController) GetTeamList(user string)   {}
