package main

import (
	"encoding/base64"
	"fmt"

	"mongo.games.com/goserver/core/basic"
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/netlib"
	"mongo.games.com/goserver/core/task"

	"mongo.games.com/game/common"
	"mongo.games.com/game/model"
	"mongo.games.com/game/protocol/welfare"
	"mongo.games.com/game/srvdata"
)

// 救济金
type CSGetReliefFundPacketFactory struct {
}

type CSGetReliefFundHandler struct {
}

func (this *CSGetReliefFundPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSGetReliefFund{}
	return pack
}

func (this *CSGetReliefFundHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSGetReliefFund Process recv ", data)
	if msg, ok := data.(*welfare.CSGetReliefFund); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSGetReliefFundHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		WelfareMgrSington.GetReliefFund(p, msg.GetIsVideo())
	}
	return nil
}

// 转动转盘
type CSGetTurnplatePacketFactory struct {
}

type CSGetTurnplateHandler struct {
}

func (this *CSGetTurnplatePacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSGetTurnplate{}
	return pack
}

func (this *CSGetTurnplateHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSGetTurnplate Process recv ", data)
	if msg, ok := data.(*welfare.CSGetTurnplate); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSGetTurnplateHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		if msg.GetIsVideo() {
			// 看视频可以再领一次
			WelfareMgrSington.GetTurnplteVideo(p)
			return nil
		}
		WelfareMgrSington.GetTurnplate(p)
	}
	return nil
}

// 累计签到
type CSGetAddupSignPacketFactory struct {
}

type CSGetAddupSignHandler struct {
}

func (this *CSGetAddupSignPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSGetAddupSign{}
	return pack
}

func (this *CSGetAddupSignHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSGetAddupSign Process recv ", data)
	if msg, ok := data.(*welfare.CSGetAddupSign); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSGetAddupSignHandler p == nil")
			return nil
		}

		WelfareMgrSington.GetAddupSign(p, msg.GetAddUpDay())
	}
	return nil
}

// 福利信息
type CSWelfaredInfoPacketFactory struct {
}

type CSWelfaredInfoHandler struct {
}

func (this *CSWelfaredInfoPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSWelfaredInfo{}
	return pack
}

func (this *CSWelfaredInfoHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSWelfaredInfo Process recv ", data)
	if _, ok := data.(*welfare.CSWelfaredInfo); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSWelfaredInfoHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		WelfareMgrSington.WelfaredInfo(p)
	}
	return nil
}

// 查看盲盒
type CSBlindBoxInfoPacketFactory struct {
}

type CSBlindBoxInfoHandler struct {
}

func (this *CSBlindBoxInfoPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSBlindBoxInfo{}
	return pack
}

func (this *CSBlindBoxInfoHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSBlindBoxInfo Process recv ", data)
	if msg, ok := data.(*welfare.CSBlindBoxInfo); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSBlindBoxInfoHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		WelfareMgrSington.BlindBoxInfo(p, msg.GetId())
	}
	return nil
}

// 购买盲盒
type CSBuyBlindBoxPacketFactory struct {
}

type CSBuyBlindBoxHandler struct {
}

func (this *CSBuyBlindBoxPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSGetBlindBox{}
	return pack
}

func (this *CSBuyBlindBoxHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSGetBlindBox Process recv ", data)
	if _, ok := data.(*welfare.CSGetBlindBox); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSBuyBlindBoxHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		//WelfareMgrSington.BuyBlindBox(p, msg.GetId(), msg.ConfigPayId)
	}
	return nil
}

// 查看首充
type CSFirstPayInfoPacketFactory struct {
}

type CSFirstPayInfoHandler struct {
}

func (this *CSFirstPayInfoPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSWelfareFirstPayData{}
	return pack
}

func (this *CSFirstPayInfoHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSWelfareFirstPayData Process recv ", data)
	if _, ok := data.(*welfare.CSWelfareFirstPayData); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSFirstPayInfoHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		WelfareMgrSington.FirstPayInfo(p)
	}
	return nil
}

// 领取首充
type CSBuyFirstPayPacketFactory struct {
}

type CSBuyFirstPayHandler struct {
}

func (this *CSBuyFirstPayPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSWelfareFirstPay{}
	return pack
}

func (this *CSBuyFirstPayHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSWelfareFirstPay Process recv ", data)
	if _, ok := data.(*welfare.CSWelfareFirstPay); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSBuyFirstPayHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		//WelfareMgrSington.BuyFirstPay(p, msg.ConfigPayId)
	}
	return nil
}

// 查看连续充值
type CSContinuousPayInfoPacketFactory struct {
}

type CSContinuousPayInfoHandler struct {
}

func (this *CSContinuousPayInfoPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSWelfareContinuousPayData{}
	return pack
}

func (this *CSContinuousPayInfoHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSWelfareContinuousPayData Process recv ", data)
	if _, ok := data.(*welfare.CSWelfareContinuousPayData); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSContinuousPayInfoHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		WelfareMgrSington.ContinuousPayInfo(p)
	}
	return nil
}

// 领取连续充值
type CSBuyContinuousPayPacketFactory struct {
}

type CSBuyContinuousPayHandler struct {
}

func (this *CSBuyContinuousPayPacketFactory) CreatePacket() interface{} {
	pack := &welfare.CSWelfareContinuousPay{}
	return pack
}

func (this *CSBuyContinuousPayHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSWelfareContinuousPay Process recv ", data)
	if _, ok := data.(*welfare.CSWelfareContinuousPay); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warnf("CSBuyContinuousPayHandler p == nil p.SnId = %v", p.SnId)
			return nil
		}
		//WelfareMgrSington.BuyContinuousPay(p, msg.ConfigPayId)
	}
	return nil
}

func CSWelfRelief(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Tracef("CSWelfRelief Process recv %v", data)
	_, ok := data.(*welfare.CSWelfareRelief)
	if !ok {
		return nil
	}

	p := PlayerMgrSington.GetPlayer(sid)
	if p == nil {
		return nil
	}

	conf := srvdata.PBDB_GameSubsidyMgr.GetData(GameSubsidyid)

	ret := &welfare.SCWelfareRelief{
		LimitNum: conf.GetLimitNum(),
		Get:      conf.GetGet(),
		Times:    conf.GetTimes(),
	}

	p.SendToClient(int(welfare.SPacketID_PACKET_SCWelfRelief), ret)
	logger.Logger.Tracef("SCWelfRelief %v", ret)
	return nil
}

func CSInviteInfo(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Tracef("CSInviteInfo Process recv %v", data)
	p := PlayerMgrSington.GetPlayer(sid)
	if p == nil {
		return nil
	}

	ul := fmt.Sprintf("%s?user=%s", model.GameParamData.InviteUrl, base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s|%d", p.Platform, p.SnId))))

	ret := &welfare.SCInviteInfo{
		Num:       p.InviteNum,
		Code:      p.InviteCode,
		InviteUrl: ul,
		Score:     p.InviteScore,
		OtherCode: p.OtherCode,
	}

	p.SendToClient(int(welfare.SPacketID_PACKET_SCInviteInfo), ret)
	logger.Logger.Tracef("SCInviteInfo %v", ret)
	return nil
}

func CSBindInvite(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Tracef("CSBindInvite Process recv %v", data)
	msg, ok := data.(*welfare.CSBindInvite)
	if !ok {
		return nil
	}
	p := PlayerMgrSington.GetPlayer(sid)
	if p == nil {
		return nil
	}
	ret := &welfare.SCBindInvite{
		OpRetCode: welfare.OpResultCode_OPRC_Error,
	}

	var inviteSnId int32
	var err error

	send := func() {
		p.SendToClient(int(welfare.SPacketID_PACKET_SCBindInvite), ret)
		logger.Logger.Tracef("SCBindInvite %v", ret)

		if ret.OpRetCode == welfare.OpResultCode_OPRC_Sucess {
			// 更新绑定关系
			LogChannelSingleton.WriteLog(&model.BindInvite{
				Platform:   p.Platform,
				SnId:       p.SnId,
				InviteSnId: inviteSnId,
			})

			TaskSubjectSingleton.Touch(common.TaskTypeBindInviter, &TaskData{SnId: p.SnId, Num: 1})
			p.InviteTask(common.InviteScoreTypeLogin, 0, 1)
		}
	}

	task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} {
		inviteSnId, err = model.GetSnIdByCode(p.Platform, msg.GetCode())
		return nil
	}), task.CompleteNotifyWrapper(func(i interface{}, t task.Task) {
		if err != nil || inviteSnId == 0 {
			logger.Logger.Errorf("GetSnIdByCode err %v %v", err, inviteSnId)
			ret.OpRetCode = welfare.OpResultCode_OPRC_NotExist
			send()
			return
		}

		if p.InviteSnId != 0 {
			ret.OpRetCode = welfare.OpResultCode_OPRC_AlreadyBind
			send()
			return
		}
		if inviteSnId == p.SnId {
			ret.OpRetCode = welfare.OpResultCode_OPRC_BindSelf
			send()
			return
		}
		pp := PlayerMgrSington.GetPlayerBySnId(inviteSnId)
		if pp != nil {
			if pp.Platform != p.Platform {
				send()
				return
			}
			if pp.InviteSnId == p.SnId {
				ret.OpRetCode = welfare.OpResultCode_OPRC_MyInvite
				send()
				return
			}
			p.InviteSnId = pp.SnId
			p.OtherCode = pp.InviteCode
			ret.OpRetCode = welfare.OpResultCode_OPRC_Sucess
			send()
			return
		}

		inviteSnId2 := int32(0)
		var err error
		task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} {
			inviteSnId2, err = model.GetPlayerInviteSnid(p.Platform, inviteSnId)
			return nil
		}), task.CompleteNotifyWrapper(func(i interface{}, t task.Task) {
			if err != nil {
				send()
				return
			}
			if inviteSnId2 == p.SnId {
				ret.OpRetCode = welfare.OpResultCode_OPRC_MyInvite
				send()
				return
			}
			p.InviteSnId = inviteSnId
			p.OtherCode = msg.GetCode()
			ret.OpRetCode = welfare.OpResultCode_OPRC_Sucess
			send()
		})).Start()
	})).Start()
	return nil
}

func init() {
	// 领取救济金
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_GETRELIEFFUND), &CSGetReliefFundHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_GETRELIEFFUND), &CSGetReliefFundPacketFactory{})
	//转动转盘
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_GETTURNPLATE), &CSGetTurnplateHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_GETTURNPLATE), &CSGetTurnplatePacketFactory{})
	//累计签到
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_GETADDUPSIGN), &CSGetAddupSignHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_GETADDUPSIGN), &CSGetAddupSignPacketFactory{})
	//福利信息
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_WELFAREINFO), &CSWelfaredInfoHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_WELFAREINFO), &CSWelfaredInfoPacketFactory{})
	//查看盲盒
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_BLINBOXINFO), &CSBlindBoxInfoHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_BLINBOXINFO), &CSBlindBoxInfoPacketFactory{})
	//购买盲盒
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_GETBLINBOX), &CSBuyBlindBoxHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_GETBLINBOX), &CSBuyBlindBoxPacketFactory{})
	//查看首充
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_FIRSTPAYINFO), &CSFirstPayInfoHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_FIRSTPAYINFO), &CSFirstPayInfoPacketFactory{})
	//领取首充
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_FIRSTPAY), &CSBuyFirstPayHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_FIRSTPAY), &CSBuyFirstPayPacketFactory{})
	//查看连续充值
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_CONTINPAYINFO), &CSContinuousPayInfoHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_CONTINPAYINFO), &CSContinuousPayInfoPacketFactory{})
	//领取连续充值
	common.RegisterHandler(int(welfare.SPacketID_PACKET_CS_WELF_CONTINPAY), &CSBuyContinuousPayHandler{})
	netlib.RegisterFactory(int(welfare.SPacketID_PACKET_CS_WELF_CONTINPAY), &CSBuyContinuousPayPacketFactory{})
	// 破产补助信息
	common.Register(int(welfare.SPacketID_PACKET_CSWelfRelief), welfare.CSWelfareRelief{}, CSWelfRelief)
	// 邀请信息
	common.Register(int(welfare.SPacketID_PACKET_CSInviteInfo), welfare.CSInviteInfo{}, CSInviteInfo)
	// 绑定信息
	common.Register(int(welfare.SPacketID_PACKET_CSBindInvite), welfare.CSBindInvite{}, CSBindInvite)
}
