using System.Collections.Generic;
using Content.Shared.Medical.SuitSensor;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Localization;

namespace Content.Client.Medical.CrewMonitoring
{
    [GenerateTypedNameReferences]
    public sealed partial class CrewMonitoringWindow : DefaultWindow
    {
        private List<Control> _rowsContent = new();

        public CrewMonitoringWindow()
        {
            RobustXamlLoader.Load(this);
        }

        public void ShowSensors(List<SuitSensorStatus> stSensors)
        {
            ClearAllSensors();

            // add a row for each sensor
            foreach (var sensor in stSensors)
            {
                // add users name and job
                // format: UserName (Job)
                var nameLabel = new Label()
                {
                    Text = $"{sensor.Name} ({sensor.Job})"
                };
                SensorsTable.AddChild(nameLabel);
                _rowsContent.Add(nameLabel);

                // add users status and damage
                // format: IsAlive (TotalDamage)
                var statusText = Loc.GetString(sensor.IsAlive ?
                    "crew-monitoring-user-interface-alive" :
                    "crew-monitoring-user-interface-dead");
                if (sensor.TotalDamage != null)
                {
                    statusText += $" ({sensor.TotalDamage})";
                }
                var statusLabel = new Label()
                {
                    Text = statusText
                };
                SensorsTable.AddChild(statusLabel);
                _rowsContent.Add(statusLabel);

                // add users positions
                // format: (x, y)
                string posText;
                if (sensor.Coordinates != null)
                {
                    // todo: add locations names (kitchen, bridge, etc)
                    var pos = sensor.Coordinates.Value.Position;
                    var x = (int) pos.X;
                    var y = (int) pos.Y;
                    posText = $"({x}, {y})";
                }
                else
                {
                    posText = Loc.GetString("crew-monitoring-user-interface-no-info");
                }
                var posLabel = new Label()
                {
                    Text = posText
                };
                SensorsTable.AddChild(posLabel);
                _rowsContent.Add(posLabel);
            }
        }

        private void ClearAllSensors()
        {
            foreach (var child in _rowsContent)
            {
               SensorsTable.RemoveChild(child);
            }
            _rowsContent.Clear();
        }
    }
}
