using Barotrauma.Extensions;
using Barotrauma.Networking;

namespace Barotrauma
{
    partial class AbandonedOutpostMission : Mission
    {
        public override int State
        {
            get { return base.State; }
            set
            {
                if (state != value)
                {
                    base.State = value;
                    if (state == HostagesKilledState && !hostagesKilledMessage.IsNullOrEmpty())
                    {
                        CreateMessageBox(string.Empty, hostagesKilledMessage);
                    }
                }
            }
        }

        public override bool DisplayAsCompleted => 
            !DisplayAsFailed &&
            State > 0 && 
            //don't display as completed mid-round if there's NPCs to rescue (mission isn't guaranteed to complete yet)
            requireRescue.None();

        public override bool DisplayAsFailed => State == HostagesKilledState;

        public override void ClientReadInitial(IReadMessage msg)
        {
            base.ClientReadInitial(msg);
            ushort targetItemCount = msg.ReadUInt16();
            for (int i = 0; i < targetItemCount; i++)
            {
                var item = Item.ReadSpawnData(msg);
                items.Add(item);
            }

            byte characterCount = msg.ReadByte();

            for (int i = 0; i < characterCount; i++)
            {
                Character character = Character.ReadSpawnData(msg);
                characters.Add(character);
                if (msg.ReadBoolean()) { requireKill.Add(character); }
                if (msg.ReadBoolean()) 
                { 
                    requireRescue.Add(character);
#if CLIENT
                    if (allowOrderingRescuees)
                    {
                        GameMain.GameSession.CrewManager?.AddCharacterToCrewList(character);
                    }
#endif
                }
                ushort itemCount = msg.ReadUInt16();
                for (int j = 0; j < itemCount; j++)
                {
                    Item.ReadSpawnData(msg);
                }
                if (character.Submarine != null && character.AIController is EnemyAIController enemyAi)
                {
                    enemyAi.UnattackableSubmarines.Add(character.Submarine);
                    if (Submarine.MainSub != null)
                    {
                        enemyAi.UnattackableSubmarines.Add(Submarine.MainSub);
                        foreach (Submarine sub in Submarine.MainSub.DockedTo)
                        {
                            enemyAi.UnattackableSubmarines.Add(sub);
                        }
                    }
                }
            }
            if (characters.Contains(null))
            {
                throw new System.Exception("Error in AbandonedOutpostMission.ClientReadInitial: character list contains null (mission: " + Prefab.Identifier + ")");
            }
            if (characters.Count != characterCount)
            {
                throw new System.Exception("Error in AbandonedOutpostMission.ClientReadInitial: character count does not match the server count (" + characters + " != " + characters.Count + "mission: " + Prefab.Identifier + ")");
            }
        }
    }
}