using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using AX.DrillOnlineServer;
using System.Text.RegularExpressions;
using AX.NetworkSystem;

public class CreateRoom : MonoBehaviour {

    public MapType mapType;
    private InputField roomName;
    private InputField roomPassword;
    private Text hint;
    // Use this for initialization
    void Start () {
        roomName = GameObject.Find("DisasterInfo/RoomName/InputField").GetComponent<InputField>();
        roomPassword = GameObject.Find("DisasterInfo/RoomPassword/InputField").GetComponent<InputField>();
        hint = transform.parent.Find("Hints").GetComponent<Text>();

        GetComponent<Button>().onClick.AddListener(CreatedRoom);
	}

    private void OnDestroy()
    {
        GetComponent<Button>().onClick.RemoveListener(CreatedRoom);
    }

    private void CreatedRoom()
    {
        if (mapType == null)
        {//没有选中灾情
            hint.text = "请选择灾情!";
            return;
        }

        if (roomName.text == "")
        {//房间名必填
            hint.text = "请输入房间名称!";
            return;
        }

        Regex rex = new Regex("^[1-9]\\d{2,5}$");

        if (roomPassword.text != "")
        {
            if (!rex.IsMatch(roomPassword.text))
            {
                hint.text = "房间密码由3-6位非0开头的数字组成!";
                return;
            }
        }

        Room room = new Room();
        room.Name = roomName.text;
        room.Password = roomPassword.text == ""? "" : SHA256HashAlgorithmHelper.GetSHA256hash(roomPassword.text);

        UserData owner = new UserData();
        owner.UserInfo = CurrentUserInfo.mySelf;
        owner.Role = CurrentUserInfo.role;
        owner.Org = CurrentUserInfo.organization;
        room.Owner = owner;

        room.Map = mapType;
        room.Mode = GameSettings.othersSettings.mode;

        NetworkManager.Default.SendRequestAsync("ROOM_CREATE_REQUEST", room);
    }

    // Update is called once per frame
    void Update () {
		
	}
}