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

public class ArrivedPowerTotal : MonoBehaviour
{ 
    public GameObject content;
    //private PowerPair arrivedPowers = new PowerPair();
    public static event Func<PowerPair, PowerPair> getArrivedPower;
    /// <summary>
    /// 记录所有已经到场的车辆
    /// </summary>
    public static Dictionary<string, List<KeyValuePair<FireCarEngine, int>>> arrivedCars = 
        new Dictionary<string, List<KeyValuePair<FireCarEngine, int>>>();
    private int trucksCount = 0;
    private int firemenCount = 0;     
    private void Awake()
    {
        if (content == null)
        {
            content = transform.parent.Find("Scroll View/Viewport/Content").gameObject;
        }       
    }
    public static void ResertArrivePowerTotal() 
    {
        arrivedCars.Clear();
    }
    void OnEnable()
    {              
        trucksCount = 0;
        firemenCount = 0;
        foreach(List<KeyValuePair<FireCarEngine,int>> carlist in arrivedCars.Values)
        {
            foreach(KeyValuePair<FireCarEngine,int> pair in carlist)
            {
                trucksCount += pair.Value;
                firemenCount += (int)pair.Key.PassengerCapacity * pair.Value;
            }
        }
        if (GameSettings.othersSettings.mode==Mode.manoeuvre) 
        {
            firemenCount = 0;
            foreach (var item in GameSettings.othersSettings.DisplayName_Num_Dic.Keys)
            {
                if (GameSettings.othersSettings.DisPlay_Arrive_Dic[item]) 
                {
                    firemenCount += GameSettings.othersSettings.DisplayName_Num_Dic[item];
                }
            }
        }
        GetComponent<Text>().text = "到场力量共" + arrivedCars.Count + "个中队" + trucksCount + "辆车," + firemenCount + "名消防员。";       
        content.GetComponent<ArrivedForceContent>().CreateItems(arrivedCars);
    }   
    /// <summary>
    /// 有新的中队车辆到场
    /// </summary>
    /// <param name="team"></param>
    /// <param name="cars"></param>
    public static void addArrivedCar(string team,List<KeyValuePair<FireCarEngine,int>> cars)
    {
        List<KeyValuePair<FireCarEngine, int>> arrivedList = new List<KeyValuePair<FireCarEngine, int>>();
        //之前已经有这个中队,将该中队已有的在途车辆提取出到typeCars
        if (arrivedCars.TryGetValue(team, out arrivedList))
        {            
            foreach (KeyValuePair<FireCarEngine, int> item in cars)
            {
                //更新
                AddCar(arrivedList, item);
            }
        }
        //之前没有这个中队
        else
        {
            arrivedCars.Add(team, cars);
        }
    }
    private static void AddCar(List<KeyValuePair<FireCarEngine, int>> typeCars, KeyValuePair<FireCarEngine, int> item)
    {
        //之前保存过这种车型
        for (int i = 0; i < typeCars.Count; i++)
        {
            if (typeCars[i].Key.TypeName == item.Key.TypeName)
            {
                typeCars[i] = new KeyValuePair<FireCarEngine, int>(typeCars[i].Key, typeCars[i].Value + item.Value);
                return;
            }
        }
        //之前没保存过这种车型
        typeCars.Add(item);
    }
}
/// <summary>
/// 参战力量、到场力量使用
/// </summary>
public class PowerPair
{
    public PowerPair()
    {
        truckMsgs = new List<TruckMessage>();
        teams = new List<string>();
        trucks = 0;
        firemen = 0;
    }
    /// <summary>
    /// 所有车辆信息
    /// </summary>
    public List<TruckMessage> truckMsgs;
    /// <summary>
    /// 所有中队
    /// </summary>
    public List<string> teams;   
    /// <summary>
    /// 参战、到场车辆计数
    /// </summary>
    public int trucks;
    /// <summary>
    /// 参战、到场消防员计数
    /// </summary>
    public int firemen;
    public void Clear()
    {
        trucks = 0;
        firemen = 0;
        truckMsgs.Clear();
    }
}