using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum RoomState
{
    None,
    Wait,
    Start,
    UnLoad,
    Loading
}
public class RoomManager : MonoBehaviour
{
    public RoomState MyState;
    public Room MyRoom;
    public GameObject StateWait;
    public GameObject StateStart;
    public GameObject StateUnLoad;
    public GameObject StateLoading;
    public bool HasPassWord;
    public GameObject Lock;
    public Image RoomShowImg;
    public Text RoomName;
    private bool HasStart = false;
    private Toggle MyToggle;

    void Start()
    {
        MyToggle = GetComponent<Toggle>();
        MyToggle.group = GetComponentInParent<ToggleGroup>();
        MyToggle.onValueChanged.AddListener(ClickRoom);
        if (!string.IsNullOrEmpty(MyRoom.Password))
        {
            HasPassWord = true;
            Lock.gameObject.SetActive(true);
        }
        string imgPath = "Common/" + MyRoom.Map.unit;
        Texture2D texture = (Texture2D)Resources.Load(imgPath);
        RoomShowImg.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        RoomName.text = MyRoom.Name;
    }

    private void ClickRoom(bool ok)
    {
        if (ok)
        {
            /*TODO:第二版资源加载中做
             * 判断当前客服端有没有要进入房间的场景或资源;若没有则下载相应的地图资源,并切换房间UI的状态
             * */

            if (LobbyDataManager.GetInstance.NowRoomId != MyRoom.Id)
            {
                LobbyDataManager.GetInstance.BindRoomMessage(this);
                CurrentUserInfo.room = MyRoom;
            }
        }
        else
        {
            if (!GetComponent<Toggle>().group.AnyTogglesOn())
            {
                LobbyDataManager.GetInstance.BindRoomMessage(null);
                CurrentUserInfo.room = null;
            }
        }
    }
    void Update()
    {
        if (MyRoom.IsDrillStart && !HasStart)
        {
            MyState = RoomState.Start;
            HasStart = true;
        }
        if (MyState != RoomState.None)
        {
            switch (MyState)
            {
                case RoomState.Wait:
                    StateWait.gameObject.SetActive(true);
                    StateStart.gameObject.SetActive(false);
                    StateUnLoad.gameObject.SetActive(false);
                    StateLoading.gameObject.SetActive(false);
                    MyState = RoomState.None;
                    break;
                case RoomState.Start:
                    StateWait.gameObject.SetActive(false);
                    StateStart.gameObject.SetActive(true);
                    StateUnLoad.gameObject.SetActive(false);
                    StateLoading.gameObject.SetActive(false);
                    MyState = RoomState.None;
                    break;
                case RoomState.UnLoad:
                    StateWait.gameObject.SetActive(false);
                    StateStart.gameObject.SetActive(false);
                    StateUnLoad.gameObject.SetActive(true);
                    StateLoading.gameObject.SetActive(false);
                    MyState = RoomState.None;
                    break;
                case RoomState.Loading:
                    StateWait.gameObject.SetActive(false);
                    StateStart.gameObject.SetActive(false);
                    StateUnLoad.gameObject.SetActive(false);
                    StateLoading.gameObject.SetActive(true);
                    //预留加载地图
                    {
                        //TODO:
                    }
                    break;
            }
        }
    }
}