using AX.Network.Protocols;
using AX.Serialization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class COMMANDCENTER_FIREMANNUM_SYNC : NetworkMessageBehaviour
{
    static CommandFiremanNumSyncData thisinfo;
    protected override void Execute(BinaryMessage message)
    {
        if(GameSettings.othersSettings.mode==Mode.manoeuvre)
        {
            var info = message.Body.Deserialize<CommandFiremanNumSyncData>();
            thisinfo = info;
            //当前客户端是调派客户端
            if (CurrentUserInfo.mySelf.Id==info.selectUserData.UserInfo.Id) 
            {
                GameSettings.othersSettings.nowperson = info.num;
                GameSettings.othersSettings.personNum += info.num;
            }
            if (!GameSettings.othersSettings.DisplayName_Num_Dic.ContainsKey(info.selectUserData.Org.DisplayName))
            {
                GameSettings.othersSettings.DisplayName_Num_Dic.Add(info.selectUserData.Org.DisplayName, info.num);
            }
            else 
            {
                GameSettings.othersSettings.DisplayName_Num_Dic[info.selectUserData.Org.DisplayName] += info.num;
            }
            if (!GameSettings.othersSettings.FireEngines_Dic.ContainsKey(info.selectUserData.Org.DisplayName))
            {
                GameSettings.othersSettings.FireEngines_Dic.Add(info.selectUserData.Org.DisplayName, info.num);
            }
            else
            {
                GameSettings.othersSettings.FireEngines_Dic[info.selectUserData.Org.DisplayName] += info.num;
            }
        }
    }
    public static void SetCar() 
    {
        
        //给新派遣的车辆分配人数
        int nowperson = thisinfo.num;
        List<KeyValuePair<FireCarEngine, bool>> carlistpire = new List<KeyValuePair<FireCarEngine, bool>>();
        //查找该中队还未分配人数的车辆,也就是本次调派的车辆
        if (GameSettings.othersSettings.DisCarList_Dic.ContainsKey(thisinfo.selectUserData.Org.DisplayName))
        {
            carlistpire = GameSettings.othersSettings.DisCarList_Dic[thisinfo.selectUserData.Org.DisplayName];
        }
        List<FireCarEngine> carlist = new List<FireCarEngine>();
        List<FireCarEngine> carlistready = new List<FireCarEngine>();
        if (carlistpire.Count > 0)
        {
            for (int i = 0; i < carlistpire.Count; i++)
            {
                if (carlistpire[i].Value == false)
                {
                    carlist.Add(carlistpire[i].Key);
                    // carlistpire.Remove(carlistpire[i]);
                }
                else 
                {
                    carlistready.Add(carlistpire[i].Key);
                }
            }
        }
      
        if (nowperson > 0)
        {
            int[] PersonNum = new int[carlist.Count];

            while (nowperson > 0)
            {
                for (int i = 0; i < carlist.Count; i++)
                {
                    if (nowperson > 0 && PersonNum[i] < carlist[i].PassengerCapacity)
                    {
                        PersonNum[i] += 1;
                        nowperson--;
                    }
                }
            }
            carlistpire.Clear();
            for (int i = 0; i < carlistready.Count; i++)
            {
                KeyValuePair<FireCarEngine, bool> carpire = new KeyValuePair<FireCarEngine, bool>(carlistready[i], true);
                carlistpire.Add(carpire);
            }
            for (int i = 0; i < carlist.Count; i++)
            {
                carlist[i].PassengerCapacity = PersonNum[i];
                KeyValuePair<FireCarEngine, bool> carpire = new KeyValuePair<FireCarEngine, bool>(carlist[i], true);
                carlistpire.Add(carpire);
            }
            GameSettings.othersSettings.DisCarList_Dic[thisinfo.selectUserData.Org.DisplayName] = carlistpire;
        }
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
        thisinfo = null;
    }
}