package controller

import (
	"fmt"
	"ktogame/models"
	"ktogame/util"
	"math/big"
	"strings"
	"time"
)

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 != "" {
		err := UpdateAvailableRewards(uc.engine, rewards, ui.Direct, false, false)
		if err != nil {
			ErrResponse(uc.Controller, err)
			return
		}
	}
	///update indirect recommend rewards
	if ui.Indirect != "" {
		err := UpdateAvailableRewards(uc.engine, rewards, ui.Indirect, false, false)
		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) GetClaimSignature() {
	addr := uc.GetString("addr")
	s := uc.GetString("token")
	user := util.GetPri(s, addr[len(addr)-16:])
	if strings.ToLower(user) != strings.ToLower(addr) {
		ErrResponse(uc.Controller, "无效地址")
		return
	}
	var ua models.UserInfo
	uc.engine.Id(user).Get(&ua)
	if ua.AvailableClaim == 0 {
		ErrResponse(uc.Controller, "暂无收益领取")
		return
	}

	format := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
	te, _ := time.ParseInLocation("2006-01-02", format, time.Local)
	// pow := decimal.NewFromFloat(float64(10)).Pow(decimal.NewFromFloat(float64(18)))
	// amount := decimal.NewFromFloat(ua.AvailableClaim).Mul(pow).BigInt().String()
	amount := big.NewInt(0).SetUint64(uint64(ua.AvailableClaim)).String()
	out1 := util.Sign(amount, strings.ToLower(user[2:]), fmt.Sprint(te.Unix()))
	sign := out1 + "-" + amount + "-" + fmt.Sprint(te.Unix())
	code := util.EnPriCode(sign, addr[len(addr)-16:])
	TxObjectResponse(uc.Controller, code)
}
func (uc *UserController) SetClaimHash() {
	addr := uc.GetString("addr")
	sign := uc.GetString("sign")
	hash := util.GetPri(sign, addr[len(addr)-16:])
	if len(hash) != 66 {
		TxObjectResponse(uc.Controller, "-1")
		return
	}

	var ua models.UserInfo
	ok, err := uc.engine.Id(addr).Get(&ua)
	if err != nil {
		TxObjectResponse(uc.Controller, "-1")
		return
	}
	if !ok {
		TxObjectResponse(uc.Controller, "-1")
		return
	}
	if ua.AvailableClaim == 0 {
		ErrResponse(uc.Controller, "暂无收益领取")
		return
	}
	amount := ua.AvailableClaim
	ua.TotalClaimed += ua.AvailableClaim
	ua.AvailableClaim = 0
	_, err = uc.engine.ID(addr).Cols("total_claimed,available_claim").Update(&ua)
	if err != nil {
		ErrResponse(uc.Controller, "-1")
		return
	}

	claimTx := models.ClaimedTxs{Addr: addr, Amount: amount, Hash: hash,
		Signature: sign, State: 0, Droped: 0, CreateTime: util.NowTimeString()}
	_, err = uc.engine.Insert(&claimTx)
	if err != nil {
		ErrResponse(uc.Controller, "set claim tx info failed!")
		return
	}
	TxObjectResponse(uc.Controller, "ok")
}
func (uc *UserController) GetUserInfo() {}
func (uc *UserController) GetTeamList() {}
