package room

import (
	"encoding/json"
	"errors"
	"strings"

	"github.com/sirupsen/logrus"
	gameQZ "nn.daxia.dev/game/qznn"
	"nn.daxia.dev/gameproto"
	"nn.daxia.dev/handler/api"
	"nn.daxia.dev/nxd"
)

type JoinRoomReq struct {
	RoomID  int32  `json:"room_id"`
	ChairID int32  `json:"chair_id"`
	Passwd  string `json:"passwd"`
}

type CreateRoomReq struct {
	Passwd     string `json:"passwd"`
	Name       string `json:"name"`
	BaseAmount int    `json:"base_amount"`
	RoomType   int32  `json:"room_type"`
}

func Join(req api.PerformReq) error {
	var err error

	userModel := req.UserModel

	joinRoomReq := JoinRoomReq{}
	err = json.Unmarshal(req.Msg, &joinRoomReq)
	if err != nil {
		logrus.Error(err)
		return err
	}

	gameInstance := gameQZ.GetInstance()
	gameInstance.Join(int32(userModel.ID))

	err = gameInstance.ChooseChair(int32(userModel.ID), int32(joinRoomReq.RoomID),
		int32(joinRoomReq.ChairID), joinRoomReq.Passwd)
	if err != nil {
		logrus.Error(err)

		nxd.FailedResponse(req.Once, uint32(userModel.ID), err.Error())
		return err
	}

	nxd.SuccessResponse(req.Once, uint32(userModel.ID), nil)
	return nil
}

func RndJoin(req api.PerformReq) error {
	userModel := req.UserModel

	gameInstance := gameQZ.GetInstance()
	gameInstance.Join(int32(userModel.ID))

	err := gameInstance.ChooseRndChair(int32(userModel.ID))
	if err != nil {
		logrus.Error(err)

		nxd.FailedResponse(req.Once, uint32(userModel.ID), err.Error())
		return err
	}

	nxd.SuccessResponse(req.Once, uint32(userModel.ID), nil)
	return nil
}

func Leave(req api.PerformReq) error {
	userModel := req.UserModel

	gameInstance := gameQZ.GetInstance()
	err := gameInstance.ChooseChair(int32(userModel.ID), 0, 0, "")
	if err != nil {
		logrus.Error(err)

		nxd.FailedResponse(req.Once, uint32(userModel.ID), err.Error())
		return err
	}

	nxd.SuccessResponse(req.Once, uint32(userModel.ID), nil)
	return nil
}

func Create(req api.PerformReq) error {
	var err error

	userModel := req.UserModel

	createRoomReq := CreateRoomReq{}
	err = json.Unmarshal(req.Msg, &createRoomReq)
	if err != nil {
		logrus.Error(err)
		nxd.FailedResponse(req.Once, uint32(userModel.ID), err.Error())
		return err
	}

	if createRoomReq.BaseAmount == 0 {
		createRoomReq.BaseAmount = 3
	}

	createRoomReq.Name = strings.TrimSpace(createRoomReq.Name)
	if createRoomReq.Name == "" {
		nxd.FailedResponse(req.Once, uint32(userModel.ID), "名称不能为空")
		return errors.New("名称不能为空")
	}

	gameInstance := gameQZ.GetInstance()
	room, err := gameInstance.CreateRoom(int32(userModel.ID), createRoomReq.RoomType, createRoomReq.Passwd,
		createRoomReq.Name, createRoomReq.BaseAmount)

	if err != nil {
		logrus.Error(err)

		nxd.FailedResponse(req.Once, uint32(userModel.ID), err.Error())
		return err
	}

	nxd.SuccessResponse(req.Once, uint32(userModel.ID), &gameproto.Room{
		ID:           uint32(room.ID),
		Name:         createRoomReq.Name,
		RoomType:     gameproto.RoomType(room.RoomType),
		BaseAmount:   float64(room.BaseAmount.IntPart()),
		ChairUserID1: uint32(room.ChairList[0].PlayerID),
		ChairUserID2: uint32(room.ChairList[1].PlayerID),
		ChairUserID3: uint32(room.ChairList[2].PlayerID),
		ChairUserID4: uint32(room.ChairList[3].PlayerID),
		ChairUserID5: uint32(room.ChairList[4].PlayerID),
	})

	return nil
}
