// roll房
package controllers

import (
	"box-gm/models"
	"box-gm/utils"
	"encoding/json"
	"fmt"
	"log"
	"time"

	"github.com/astaxie/beego"
)

type RollController struct {
	beego.Controller
}

func (c *RollController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	c.Data["username"] = userinfo.(*LoginInfo).Username
	c.Data["token"] = models.GetToken()
	userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
	if (userPermission & (1 << uint(models.ModelSystem))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	action := c.GetString("action")
	if action == "update" {
		models.InitRollInfo()
		c.Data["json"] = &map[string]interface{}{"status": true}
		c.ServeJSON()
		return
	}

	c.Data["AwardList"] = models.GetRollAwardInfo()
	c.Data["infoList"] = models.GetRollInfoList()
	c.Data["ConditionList"] = models.GetRollConditionList()
	c.Data["ItemList"] = models.GetItemInfoList()
	c.TplName = "roll.tpl"
}

func (c *RollController) Post() {
	userinfo := c.GetSession("user")
	log.Printf("RollController Post userinfo[%v]", userinfo)
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	name := c.GetString("name")
	bandShareCode := c.GetString("bandShareCode")
	showStartTime := c.GetString("showStartTime")
	signupStartTime := c.GetString("signupStartTime")
	signupEndTime := c.GetString("signupEndTime")
	awardTime := c.GetString("awardTime")
	conditionList := c.GetString("conditionList")
	awardId, _ := c.GetInt("awardId")

	log.Printf("RollController Post conditionList[%s]", conditionList)
	var _conditions []*models.RollConditionInfo
	err := json.Unmarshal([]byte(conditionList), &_conditions)
	if err != nil {
		log.Printf("RollController Post conditionList[%s] err[%v]", conditionList, err)
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}
	for _, v := range _conditions {
		log.Printf("RollController Post _conditions Conditions[%s] CValue[%d]", v.Conditions, v.CValue)
	}
	itemIdList := models.GetRollAwardItemIdList(awardId)
	if itemIdList == nil {
		log.Printf("RollController Post awardId[%d] 奖励列表空", awardId)
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "奖励列表空"}
		c.ServeJSON()
		return
	}
	_ShowStartTime := utils.GetTime64(showStartTime)
	_SignupStartTime := utils.GetTime64(signupStartTime)
	_SignupEndTime := utils.GetTime64(signupEndTime)
	_AwardTime := utils.GetTime64(awardTime)
	currTime := time.Now().Unix()
	if _ShowStartTime > _ShowStartTime {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "展示时间超过报名开始时间"}
		c.ServeJSON()
		return
	}
	if currTime > _SignupEndTime {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "报名结束时间错误！已报名结束"}
		c.ServeJSON()
		return
	}
	if currTime > _AwardTime || _ShowStartTime > _AwardTime || _SignupEndTime > _AwardTime {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "开奖时间错误！已开奖"}
		c.ServeJSON()
		return
	}
	// 奖励方案道具列表
	u := new(models.RollInfo)
	u.Id = models.CreateRollId()
	u.Name = name
	u.BandShareCode = bandShareCode
	u.ShowStartTime = _ShowStartTime
	u.SignupStartTime = _SignupStartTime
	u.SignupEndTime = _SignupEndTime
	u.AwardTime = _AwardTime
	u.Conditions = _conditions
	u.ItemIdList = itemIdList

	err = models.AddRollInfo(u)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "新增roll房成功"}
	c.ServeJSON()
}

func (c *RollController) Put() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	id, _ := c.GetInt("id")
	log.Printf("RollController Put id[%d]", id)
	hf := models.GetRollInfoById(id)
	if hf == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:奖池方案不存在"}
		c.ServeJSON()
		return
	}
	currTime := time.Now().Unix()
	if currTime >= hf.ShowStartTime {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "已开始展示给玩家，请勿变动"}
		c.ServeJSON()
		return
	}
	name := c.GetString("name")
	bandShareCode := c.GetString("bandShareCode")
	showStartTime := c.GetString("showStartTime")
	signupStartTime := c.GetString("signupStartTime")
	signupEndTime := c.GetString("signupEndTime")
	awardTime := c.GetString("awardTime")
	conditionList := c.GetString("conditionList")
	awardId, _ := c.GetInt("awardId")

	var _conditions []*models.RollConditionInfo
	err := json.Unmarshal([]byte(conditionList), &_conditions)
	if err != nil {
		log.Printf("RollController Post conditionList[%s] err[%v]", conditionList, err)
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}
	itemIdList := models.GetRollAwardItemIdList(awardId)
	if itemIdList == nil {
		log.Printf("RollController Post awardId[%d] 奖励列表空", awardId)
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "奖励列表空"}
		c.ServeJSON()
		return
	}

	hf.Name = name
	hf.BandShareCode = bandShareCode
	hf.ShowStartTime = utils.GetTime64(showStartTime)
	hf.SignupStartTime = utils.GetTime64(signupStartTime)
	hf.SignupEndTime = utils.GetTime64(signupEndTime)
	hf.AwardTime = utils.GetTime64(awardTime)
	hf.Conditions = _conditions
	hf.ItemIdList = itemIdList

	err = models.UpdateRollInfo(hf)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
		c.ServeJSON()
		return
	}
	c.Data["json"] = &map[string]interface{}{"status": true, "info": fmt.Sprintf("修改roll房[%d]成功", id)}
	c.ServeJSON()
}

func (c *RollController) Delete() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	id, _ := c.GetInt("id")
	log.Printf("RollController Post Delete id[%d]", id)
	hf := models.GetRollInfoById(id)
	if hf == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "修改失败:roll房不存在"}
		c.ServeJSON()
		return
	}
	currTime := time.Now().Unix()
	if currTime >= hf.ShowStartTime {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "已开始展示给玩家，请勿变动"}
		c.ServeJSON()
		return
	}

	models.DelRollInfoById(id)

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "删除roll房成功"}
	c.ServeJSON()
}
