package blackjack

import (
	"git.lxtend.com/lixiangwuxian/qqbot/constants"
	"git.lxtend.com/lixiangwuxian/qqbot/handler"
	"git.lxtend.com/lixiangwuxian/qqbot/handler/blackjack/controller"
	"git.lxtend.com/lixiangwuxian/qqbot/handler/blackjack/core"
	"git.lxtend.com/lixiangwuxian/qqbot/handler/blackjack/view"
	"git.lxtend.com/lixiangwuxian/qqbot/model"
	"git.lxtend.com/lixiangwuxian/qqbot/util"
)

var userGameMap map[string]*core.BackJackGame

func init() {
	handler.RegisterHandler("bj", blackJack, constants.LEVEL_USER)
	handler.RegisterHelpInform("bj", "blackjack", "bj start 开始玩黑杰克")
	userGameMap = make(map[string]*core.BackJackGame)
}

func blackJack(msg model.Message) *model.Reply {
	tokens := util.SplitN(msg.RawMsg, 2)
	if len(tokens) < 2 {
		return &model.Reply{
			ReplyMsg:       "Invalid command",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}

	if tokens[1] == "exit" {
		delete(userGameMap, util.From(msg.GroupInfo.GroupId, msg.UserId))
		return &model.Reply{
			ReplyMsg:       "Bye",
			ReferOriginMsg: false,
			FromMsg:        msg,
		}
	}

	if tokens[1] == "start" {
		userGameMap[util.From(msg.GroupInfo.GroupId, msg.UserId)] = core.NewBlackJackGame(controller.NewBlackJackSimulator(), view.NewLiteralViewer(controller.NewBlackJackSimulator()))
	}
	if _, ok := userGameMap[util.From(msg.GroupInfo.GroupId, msg.UserId)]; !ok {
		return &model.Reply{
			ReplyMsg:       "Please start a game first",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}
	}
	response, _ := userGameMap[util.From(msg.GroupInfo.GroupId, msg.UserId)].AddCommand(tokens[1])
	handler.RegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId, blackJackWithNoBj)
	if response[len(response)-1:] == "\n" {
		response = response[:len(response)-1]
	}
	return &model.Reply{
		ReplyMsg:       response,
		ReferOriginMsg: false,
		FromMsg:        msg,
	}
}

func blackJackWithNoBj(msg model.Message) (reply *model.Reply, ok bool) {
	if msg.RawMsg == "exit" {
		delete(userGameMap, util.From(msg.GroupInfo.GroupId, msg.UserId))
		handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId)
		return &model.Reply{
			ReplyMsg:       "Bye",
			ReferOriginMsg: false,
			FromMsg:        msg,
		}, true
	}

	if msg.RawMsg == "start" {
		userGameMap[util.From(msg.GroupInfo.GroupId, msg.UserId)] = core.NewBlackJackGame(controller.NewBlackJackSimulator(), view.NewLiteralViewer(controller.NewBlackJackSimulator()))
	}

	if _, ok := userGameMap[util.From(msg.GroupInfo.GroupId, msg.UserId)]; !ok {
		return &model.Reply{
			ReplyMsg:       "Please start a game first",
			ReferOriginMsg: true,
			FromMsg:        msg,
		}, true
	}
	response, err := userGameMap[util.From(msg.GroupInfo.GroupId, msg.UserId)].AddCommand(msg.RawMsg)
	if err != nil {
		if err.Error() == "invalid command" {
			return &model.Reply{
				ReplyMsg:       "",
				ReferOriginMsg: false,
				FromMsg:        msg,
			}, false
		} else if err.Error() == "game over" {
			delete(userGameMap, util.From(msg.GroupInfo.GroupId, msg.UserId))
			handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId)
		}
	}
	if len(response) > 1 && response[len(response)-1:] == "\n" {
		response = response[:len(response)-1]
	}
	return &model.Reply{
		ReplyMsg:       response,
		ReferOriginMsg: false,
		FromMsg:        msg,
	}, true
}
