package main

import (
	"common/beegomap"
	"common/logger"
	//	"fmt"
	"log"
	"math/rand"
	"net"
	"time"
)

type RoomInfo struct {
	rid uint32
	ids []uint32
}

const (
	STATE_PLAYER_INIT     = 0 //init
	STATE_PLAYER_LOGINED  = 1 //after login
	STATE_PLAYER_ENTERED  = 2 //in room
	STATE_PLAYER_ON_TABLE = 3 //on table

	STATE_PLAYER_ON_TABLE_SitDown = 4 //sit down
	STATE_PLAYER_ON_TABLE_SitOut  = 5 //sit out

	BUYIN_AMOUNT = 1000

	Min_Message_Size = 16
)

type PlayerInfo struct {
	uid       uint32
	rid       uint32
	seq       uint32
	conn      net.Conn
	isHost    bool
	state     uint32
	leftMoney uint32
	buyin     uint32
	ActionSeq int32
	roundBet  uint32
	seatid    int32

	startRecordSeq bool
	recordSeq      int

	valid          bool
	lastCheckTime  int64
	enterStateTime int64
	isObserver     bool
}

var (
	m_map_rooms        *beegomap.BeeMap
	m_map_players      *beegomap.BeeMap
	m_map_club_creater *beegomap.BeeMap
)

func init() {
	m_map_rooms = beegomap.NewBeeMap()
	m_map_players = beegomap.NewBeeMap()
	m_map_club_creater = beegomap.NewBeeMap()
}

func Start(roomcount, invertal int) {
	//init all host player & conn
	InitAllHost(roomcount)

	HostLogin(invertal)
}

func InitAllHost(roomcount int) {
	for i:=0;i<roomcount;i++ {

		uid := i
			conn := getTcpClient(uint32(uid))
			var player PlayerInfo
			player.uid = uint32(uid)
			player.rid = 0
			player.conn = conn
			player.isHost = true
			m_map_players.Set(uint32(uid), &player)
			m_map_club_creater.Set(uint32(uid), &player)
			log.Println("m_map_players.Set(uint32(uid), &player)-player.isHost:", player.isHost)
			break

	}
}

func HostLogin(interval int) {
	items := m_map_players.Items()
	for _, v := range items {
		player := v.(*PlayerInfo)
		SendLogin(player)
		StartPlayer(player)
		time.Sleep(time.Duration(interval))


	}
}

func StartPlayer(player *PlayerInfo) {
	go clientRecvLoop(player)
}

func GetPlayer(uid uint32) *PlayerInfo {
	v := m_map_players.Get(uid)
	if v == nil {
		logger.Info("get uid:%+v but not found playerid", uid)
		return nil
	}
	player := v.(*PlayerInfo)
	return player
}
func removePlayerFromRoom(uid, rid uint32) {
	v := m_map_rooms.Get(rid)
	if v == nil {
		return
	}
	rm := v.(*RoomInfo)
	for i, id := range rm.ids {
		if uint32(id) == uid {
			rm.ids = append(rm.ids[0:i], rm.ids[i+1:]...)
			break
		}
	}
}
func checkPlayer(player *PlayerInfo) {
	if player.isObserver {
		checkObserver(player)
		return
	}

	now := time.Now().Unix()
	if now < player.lastCheckTime+5 || player.valid == false || player.rid <= 0 {
		return
	}
	player.lastCheckTime = now
	RandomGet(player)
	//if RandomGet(player) {
	//	return
	//}

	switch player.state {
	case STATE_PLAYER_INIT:
		return
	case STATE_PLAYER_LOGINED:
		//		ActionWhenLogined(player)
		return
	case STATE_PLAYER_ENTERED:
		ActionWhenEntered(player)
		return
	case STATE_PLAYER_ON_TABLE_SitDown:
		ActionWhenSitDown(player)
	case STATE_PLAYER_ON_TABLE_SitOut:
		ActionWhenSitout(player)
	default:
		return
	}
}

func ActionWhenEntered(player *PlayerInfo) {
	//已经从桌上站起来了，随机 退出房间 或者坐下
	now := time.Now().Unix()
	if now < player.enterStateTime+20 {
		return
	}
	r := rand.Intn(1000)
	if r < 100 {
		//exit room
		logger.Info("Checking.......rid:%v uid:%d entered goto leave room", player.rid, player.uid)
		//SendLeaveRoomRequest(player)
	} else if r < 900 {
		//sit down
		logger.Info("Checking.......rid:%v uid:%d entered goto sitdown", player.rid, player.uid)
		//SendSitDown(player)
	} else {
		//do nothing
	}
}
func ActionWhenSitDown(player *PlayerInfo) {
	//已经坐下，随机站起，退出房间或者sitout
	now := time.Now().Unix()
	if now < player.enterStateTime+120 {
		return
	}
	r := rand.Intn(1000)
	if r < 50 {
		//standup
		logger.Info("Checking.......rid:%v uid:%d sitdown goto standup", player.rid, player.uid)
		//SendStandupRequest(player)
	} else if r < 100 {
		//exit room
		logger.Info("Checking.......rid:%v uid:%d sitdown goto leave room", player.rid, player.uid)
		//SendLeaveRoomRequest(player)
	} else if r < 150 {
		//sitout
		logger.Info("Checking.......rid:%v uid:%d sitdown goto sitout", player.rid, player.uid)
		//SendSitoutRequest(player)
	} else if r < 200 {
		// force standup
		//SendForceStandup(player)
	} else if r < 250 {
		//check out and leave (结算离桌)
		//SendCheckoutAndLeave(player)
	} else if r < 300 {
		//send buy out
		//SendBuyout(player)
	} else {
		//do nothing
	}
}
func ActionWhenSitout(player *PlayerInfo) {
	//已经sitout，随机站起，退出房间或坐回
	now := time.Now().Unix()
	if now < player.enterStateTime+5 {
		return
	}
	r := rand.Intn(1000)
	if r < 50 {
		//sitout
		logger.Info("Checking.......rid:%v uid:%d sitout goto standup", player.rid, player.uid)
		//SendStandupRequest(player)
	} else if r < 100 {
		//exit room
		logger.Info("Checking.......rid:%v uid:%d sitout goto leave room", player.rid, player.uid)
		//SendLeaveRoomRequest(player)
	} else if r < 900 {
		//sit back
		logger.Info("Checking.......rid:%v uid:%d sitout goto back position", player.rid, player.uid)
		//SendBackPosition(player)
	} else {
		//do nothing
	}
}

func RandomGet(player *PlayerInfo) bool {
	//	r := rand.Intn(1000)
	//	if r < 100 {
	//		//get room situation
	//		//SendRoomSituation(player)
	//	} else if r < 200 {
	//		//
	//		//DoInteractiveExpression(player)
	//	} else if r < 300 {
	//		//send chat
	//		//SendChat(player)
	//	} else if r < 400 {
	//		//get snapshot
	//		//SendSnapshotRequest(player)
	//	} else {
	//		//do nothing
	//		return false
	//	}
	return true
}

func getRoomRandomSeatid(rid uint32) int32 {
	v := m_map_rooms.Get(rid)
	if v == nil {
		return 0
	}
	room, _ := v.(*RoomInfo)
	r := rand.Intn(len(room.ids))
	if r < 0 {
		return -1
	}
	uid := room.ids[r]
	p := m_map_players.Get(uid)
	player, _ := p.(*PlayerInfo)
	return player.seatid
}
