using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImGuiNET;
using skyscraper5.Skyscraper;
using skyscraper5.Skyscraper.Scraper.Storage;
using skyscraper8.Skyscraper.Scraper.Storage;

namespace SDL2Demo.Forms
{
    internal class SatellitesConfigurationWindow : IRenderable
    {
        public SatellitesConfigurationWindow(DataStorage storage)
        {
            _storage = storage;
            lnbPopupUuid = Guid.NewGuid().ToString();
            name = "";
            memoryTableUuid = Guid.NewGuid().ToString();
            allPositions = storage.UiSatellitesListAll();
            
        }

        private string lnbPopupUuid;
        private string memoryTableUuid;
        private List<SatellitePositionEntity> allPositions;
        private readonly DataStorage _storage;


        private string name;
        private float degrees;
        private int cardinalDirection;

        private void CheckBounds()
        {
            if (degrees < 0.0)
                degrees = 0.0f;
            if (degrees > 180.0)
                degrees = 180.0f;
        }
        private bool SaveButtonEnabled()
        {
            if (string.IsNullOrEmpty(name))
                return false;

            if (string.IsNullOrWhiteSpace(name))
                return false;
            
            if (allPositions.Count == 0)
                return true;

            int currentChecksum = SatellitePositionEntity.GetChecksum(degrees);
            foreach (SatellitePositionEntity satellitePosition in allPositions)
            {
                if (currentChecksum == satellitePosition.Checksum)
                    return false;
            }

            return true;
        }
        public void Render()
        {
            bool pOpen = true;
            ImGui.Begin("Configure satellite positions", ref pOpen);

            if (ImGui.TreeNode("Add satellite position"))
            {
                ImGui.InputText("Name", ref name, 255);
                ImGui.InputFloat("Position", ref degrees, 0.1f);
                
                CheckBounds();
                
                ImGui.SameLine();
                ImGui.BeginDisabled(!SaveButtonEnabled());
                if (ImGui.Button("Add"))
                {
                    SatellitePositionEntity newPosition = new SatellitePositionEntity(degrees, name);
                    allPositions.Add(newPosition);
                    _storage.UiSatellitesAdd(newPosition);
                }

                ImGui.EndDisabled();
            }

            ImGui.Separator();

            ImGui.BeginTable(memoryTableUuid, 4);
            foreach (SatellitePositionEntity satellitePosition in allPositions)
            {
                ImGui.TableNextRow();
                ImGui.TableSetColumnIndex(0);
                ImGui.Text(satellitePosition.name);
                ImGui.TableSetColumnIndex(1);
                ImGui.Text(String.Format("{0:0.0}° {1}", satellitePosition.angle, satellitePosition.GetCardinalDirectionAsChar()));
                ImGui.TableSetColumnIndex(2);
                ImGui.Text("");
                
                ImGui.TableSetColumnIndex(3);
                ImGui.PushID(String.Format("RemoveSat {0}", satellitePosition.Checksum));
                if (ImGui.Button("Remove"))
                {
                    _storage.UiSatellitesDelete(satellitePosition);
                    allPositions.Remove(satellitePosition);
                    break;
                }
                ImGui.PopID();
            }
            ImGui.EndTable();

            ImGui.End();

            if (!pOpen)
                Closed = true;
        }

        public bool Closed { get; set; }
    }
}
