<commit_before>﻿using SMT.EVEData;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SMT
{
    /// <summary>
    /// Interaction logic for RegionControl.xaml
    /// </summary>
    public partial class RegionControl : UserControl, INotifyPropertyChanged
    {
        public EVEData.MapRegion Region { get; set; }
        public MapConfig MapConf { get; set; }
        public EveManager EM { get; set; }
        public AnomManager ANOMManager { get; set; }
        public string SelectedSystem { get; set; }

        // Constant Colours
        private Brush StandingVBadBrush  = new SolidColorBrush(Color.FromArgb(110, 148, 5, 5));
        private Brush StandingBadBrush   = new SolidColorBrush(Color.FromArgb(110, 196, 72, 6));
        private Brush StandingNeutBrush  = new SolidColorBrush(Color.FromArgb(110, 140, 140, 140));
        private Brush StandingGoodBrush  = new SolidColorBrush(Color.FromArgb(110, 43, 101, 196));
        private Brush StandingVGoodBrush = new SolidColorBrush(Color.FromArgb(110, 5, 34, 120));



        private Character m_ActiveCharacter;
        public Character ActiveCharacter
        {
            get
            {
                return m_ActiveCharacter;
            }
            set
            {
                m_ActiveCharacter = value;
                OnPropertyChanged("ActiveCharacter");
            }
        }


        // Store the Dynamic Map elements so they can seperately be cleared
        private List<System.Windows.UIElement> DynamicMapElements;

        // Timer to Re-draw the map
        private System.Windows.Threading.DispatcherTimer uiRefreshTimer;


        // Map Controls
        private double m_ESIOverlayScale = 1.0f;
        private bool m_ShowNPCKills = false;
        private bool m_ShowPodKills = false;
        private bool m_ShowShipKills = false;
        private bool m_ShowShipJumps = false;
        private bool m_ShowJumpBridges = true;
        private bool m_ShowStandings = false;
        private bool m_ShowSov = false;
        private bool m_ShowSystemSecurity = false;



        public bool ShowStandings
        {
            get
            {
                return m_ShowStandings;
            }
            set
            {
                m_ShowStandings = value;
                OnPropertyChanged("ShowStandings");
            }
        }

        public bool ShowSov
        {
            get
            {
                return m_ShowSov;
            }
            set
            {
                m_ShowSov = value;
                OnPropertyChanged("ShowSov");
            }
        }


        public bool ShowSystemSecurity
        {
            get
            {
                return m_ShowSystemSecurity;
            }
            set
            {
                m_ShowSystemSecurity = value;
                OnPropertyChanged("ShowSystemSecurity");
            }
        }




        public bool ShowJumpBridges
        {
            get
            {
                return m_ShowJumpBridges;
            }
            set
            {
                m_ShowJumpBridges = value;
                OnPropertyChanged("ShowJumpBridges");
            }
        }


        public double ESIOverlayScale
        {
            get
            {
                return m_ESIOverlayScale;
            }
            set
            {
                m_ESIOverlayScale = value;
                OnPropertyChanged("ESIOverlayScale");
            }
        }


        public bool ShowNPCKills
        {
            get
            {
                return m_ShowNPCKills;
            }

            set
            {
                m_ShowNPCKills = value;

                if (m_ShowNPCKills)
                {
                    ShowPodKills = false;
                    ShowShipKills = false;
                    ShowShipJumps = false;
                }

                OnPropertyChanged("ShowNPCKills");
            }
        }

        public bool ShowPodKills
        {
            get
            {
                return m_ShowPodKills;
            }

            set
            {
                m_ShowPodKills = value;
                if (m_ShowPodKills)
                {
                    ShowNPCKills = false;
                    ShowShipKills = false;
                    ShowShipJumps = false;
                }

                OnPropertyChanged("ShowPodKills");
            }
        }

        public bool ShowShipKills
        {
            get
            {
                return m_ShowShipKills;
            }

            set
            {
                m_ShowShipKills = value;
                if (m_ShowShipKills)
                {
                    ShowNPCKills = false;
                    ShowPodKills = false;
                    ShowShipJumps = false;
                }

                OnPropertyChanged("ShowShipKills");
            }
        }

        public bool ShowShipJumps
        {
            get
            {
                return m_ShowShipJumps;
            }

            set
            {
                m_ShowShipJumps = value;
                if (m_ShowShipJumps)
                {
                    ShowNPCKills = false;
                    ShowPodKills = false;
                    ShowShipKills = false;
                }

                OnPropertyChanged("ShowShipJumps");
            }
        }

        public bool FollowCharacter
        {
            get
            {
                return FollowCharacterChk.IsChecked.Value;
            }
            set
            {
                FollowCharacterChk.IsChecked = value;
            }
        }





        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }




        /// <summary>
        /// Redraw the map
        /// </summary>
        /// <param name="FullRedraw">Clear all the static items or not</param>
        public void ReDrawMap(bool FullRedraw = false)
        {
            if (ActiveCharacter != null && FollowCharacter == true)
            {
                HandleCharacterSelectionChange();
            }



            if (FullRedraw)
            {
                // reset the background
                MainCanvasGrid.Background = new SolidColorBrush(MapConf.ActiveColourScheme.MapBackgroundColour);
                MainCanvas.Background = new SolidColorBrush(MapConf.ActiveColourScheme.MapBackgroundColour);
                MainZoomControl.Background = new SolidColorBrush(MapConf.ActiveColourScheme.MapBackgroundColour);

                MainCanvas.Children.Clear();

                // re-add the static content
                AddSystemsToMap();
            }
            else
            {
                // remove anything temporary
                foreach (UIElement uie in DynamicMapElements)
                {
                    MainCanvas.Children.Remove(uie);
                }
                DynamicMapElements.Clear();
            }

            AddCharactersToMap();
            AddDataToMap();
            AddSystemIntelOverlay();
            AddHighlightToSystem(SelectedSystem);
            AddRouteToMap();
            AddTheraSystemsToMap();
        }


        private struct GateHelper
        {
            public EVEData.MapSystem from { get; set; }
            public EVEData.MapSystem to { get; set; }
        }


        private const double SYSTEM_SHAPE_SIZE = 18;
        private const double SYSTEM_SHAPE_OFFSET = SYSTEM_SHAPE_SIZE / 2;
        private const double SYSTEM_TEXT_TEXT_SIZE= 6;
        private const double SYSTEM_TEXT_X_OFFSET = 10;
        private const double SYSTEM_TEXT_Y_OFFSET = 2;
        private const double SYSTEM_REGION_TEXT_X_OFFSET = 5;
        private const double SYSTEM_REGION_TEXT_Y_OFFSET = SYSTEM_TEXT_Y_OFFSET + SYSTEM_TEXT_TEXT_SIZE + 2;

        private const int SYSTEM_Z_INDEX = 20;
        private const int SYSTEM_LINK_INDEX = 19;

        /// <summary>
        /// Initialise the control
        /// </summary>
        public void Init()
        {
            EM = EVEData.EveManager.GetInstance();
            SelectedSystem = string.Empty;

            DynamicMapElements = new List<UIElement>();

            CharacterDropDown.ItemsSource = EM.LocalCharacters;
            ActiveCharacter = null;

            RegionSelectCB.ItemsSource = EM.Regions;
            SelectRegion(MapConf.DefaultRegion);

            uiRefreshTimer = new System.Windows.Threading.DispatcherTimer();
            uiRefreshTimer.Tick += UiRefreshTimer_Tick; ;
            uiRefreshTimer.Interval = new TimeSpan(0, 0, 2);
            uiRefreshTimer.Start();

            ToolBoxCanvas.DataContext = this;

            PropertyChanged += MapObjectChanged;
        }

        /// <summary>
        /// UI Refresh Timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UiRefreshTimer_Tick(object sender, EventArgs e)
        {
            ReDrawMap(false);
        }

        public void SelectSystem(string name, bool changeRegion = false)
        {
            EVEData.System sys = EM.GetEveSystem(name);

            if(sys == null)
            {
                return;
            }

            if(changeRegion && !Region.IsSystemOnMap(name))
            {
                SelectRegion(sys.Region);
            }


            foreach (EVEData.MapSystem es in Region.MapSystems.Values.ToList())
            {
                if (es.Name == name)
                {
                    SystemDropDownAC.SelectedItem = es;
                    SelectedSystem = es.Name;
                    AddHighlightToSystem(name);

                    break;
                }
            }


            // now setup the anom data


            EVEData.AnomData system = ANOMManager.GetSystemAnomData(name);
            ANOMManager.ActiveSystem = system;

            ///AnomSigList.ItemsSource = system.Anoms.Values;


        }



        /// <summary>
        /// Select A Region
        /// </summary>
        /// <param name="regionName">Region to Select</param>
        public void SelectRegion(string regionName)
        {
            // check we havent selected the same system
            if(Region !=null && Region.Name == regionName)
            {
                return;
            }

            // check its a valid system
            EVEData.MapRegion mr = EM.GetRegion(regionName);
            if(mr == null)
            {
                return;
            }

            // update the selected region
            Region = mr;
            

            List<EVEData.MapSystem> newList = Region.MapSystems.Values.ToList().OrderBy(o => o.Name).ToList();
            SystemDropDownAC.ItemsSource = newList;

            ReDrawMap(true);

            // select the item in the dropdown
            RegionSelectCB.SelectedItem = Region;
        }

        /// <summary><commit_msg>fixed alliance id's not updating<commit_after>﻿using SMT.EVEData;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SMT
{
    /// <summary>
    /// Interaction logic for RegionControl.xaml
    /// </summary>
    public partial class RegionControl : UserControl, INotifyPropertyChanged
    {
        public EVEData.MapRegion Region { get; set; }
        public MapConfig MapConf { get; set; }
        public EveManager EM { get; set; }
        public AnomManager ANOMManager { get; set; }
        public string SelectedSystem { get; set; }

        // Constant Colours
        private Brush StandingVBadBrush  = new SolidColorBrush(Color.FromArgb(110, 148, 5, 5));
        private Brush StandingBadBrush   = new SolidColorBrush(Color.FromArgb(110, 196, 72, 6));
        private Brush StandingNeutBrush  = new SolidColorBrush(Color.FromArgb(110, 140, 140, 140));
        private Brush StandingGoodBrush  = new SolidColorBrush(Color.FromArgb(110, 43, 101, 196));
        private Brush StandingVGoodBrush = new SolidColorBrush(Color.FromArgb(110, 5, 34, 120));



        private Character m_ActiveCharacter;
        public Character ActiveCharacter
        {
            get
            {
                return m_ActiveCharacter;
            }
            set
            {
                m_ActiveCharacter = value;
                OnPropertyChanged("ActiveCharacter");
            }
        }


        // Store the Dynamic Map elements so they can seperately be cleared
        private List<System.Windows.UIElement> DynamicMapElements;

        // Timer to Re-draw the map
        private System.Windows.Threading.DispatcherTimer uiRefreshTimer;


        // Map Controls
        private double m_ESIOverlayScale = 1.0f;
        private bool m_ShowNPCKills = false;
        private bool m_ShowPodKills = false;
        private bool m_ShowShipKills = false;
        private bool m_ShowShipJumps = false;
        private bool m_ShowJumpBridges = true;
        private bool m_ShowStandings = false;
        private bool m_ShowSov = false;
        private bool m_ShowSystemSecurity = false;



        public bool ShowStandings
        {
            get
            {
                return m_ShowStandings;
            }
            set
            {
                m_ShowStandings = value;
                OnPropertyChanged("ShowStandings");
            }
        }

        public bool ShowSov
        {
            get
            {
                return m_ShowSov;
            }
            set
            {
                m_ShowSov = value;
                OnPropertyChanged("ShowSov");
            }
        }


        public bool ShowSystemSecurity
        {
            get
            {
                return m_ShowSystemSecurity;
            }
            set
            {
                m_ShowSystemSecurity = value;
                OnPropertyChanged("ShowSystemSecurity");
            }
        }




        public bool ShowJumpBridges
        {
            get
            {
                return m_ShowJumpBridges;
            }
            set
            {
                m_ShowJumpBridges = value;
                OnPropertyChanged("ShowJumpBridges");
            }
        }


        public double ESIOverlayScale
        {
            get
            {
                return m_ESIOverlayScale;
            }
            set
            {
                m_ESIOverlayScale = value;
                OnPropertyChanged("ESIOverlayScale");
            }
        }


        public bool ShowNPCKills
        {
            get
            {
                return m_ShowNPCKills;
            }

            set
            {
                m_ShowNPCKills = value;

                if (m_ShowNPCKills)
                {
                    ShowPodKills = false;
                    ShowShipKills = false;
                    ShowShipJumps = false;
                }

                OnPropertyChanged("ShowNPCKills");
            }
        }

        public bool ShowPodKills
        {
            get
            {
                return m_ShowPodKills;
            }

            set
            {
                m_ShowPodKills = value;
                if (m_ShowPodKills)
                {
                    ShowNPCKills = false;
                    ShowShipKills = false;
                    ShowShipJumps = false;
                }

                OnPropertyChanged("ShowPodKills");
            }
        }

        public bool ShowShipKills
        {
            get
            {
                return m_ShowShipKills;
            }

            set
            {
                m_ShowShipKills = value;
                if (m_ShowShipKills)
                {
                    ShowNPCKills = false;
                    ShowPodKills = false;
                    ShowShipJumps = false;
                }

                OnPropertyChanged("ShowShipKills");
            }
        }

        public bool ShowShipJumps
        {
            get
            {
                return m_ShowShipJumps;
            }

            set
            {
                m_ShowShipJumps = value;
                if (m_ShowShipJumps)
                {
                    ShowNPCKills = false;
                    ShowPodKills = false;
                    ShowShipKills = false;
                }

                OnPropertyChanged("ShowShipJumps");
            }
        }

        public bool FollowCharacter
        {
            get
            {
                return FollowCharacterChk.IsChecked.Value;
            }
            set
            {
                FollowCharacterChk.IsChecked = value;
            }
        }





        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }




        /// <summary>
        /// Redraw the map
        /// </summary>
        /// <param name="FullRedraw">Clear all the static items or not</param>
        public void ReDrawMap(bool FullRedraw = false)
        {
            if (ActiveCharacter != null && FollowCharacter == true)
            {
                HandleCharacterSelectionChange();
            }



            if (FullRedraw)
            {
                // reset the background
                MainCanvasGrid.Background = new SolidColorBrush(MapConf.ActiveColourScheme.MapBackgroundColour);
                MainCanvas.Background = new SolidColorBrush(MapConf.ActiveColourScheme.MapBackgroundColour);
                MainZoomControl.Background = new SolidColorBrush(MapConf.ActiveColourScheme.MapBackgroundColour);

                MainCanvas.Children.Clear();

                // re-add the static content
                AddSystemsToMap();
            }
            else
            {
                // remove anything temporary
                foreach (UIElement uie in DynamicMapElements)
                {
                    MainCanvas.Children.Remove(uie);
                }
                DynamicMapElements.Clear();
            }

            AddCharactersToMap();
            AddDataToMap();
            AddSystemIntelOverlay();
            AddHighlightToSystem(SelectedSystem);
            AddRouteToMap();
            AddTheraSystemsToMap();
        }


        private struct GateHelper
        {
            public EVEData.MapSystem from { get; set; }
            public EVEData.MapSystem to { get; set; }
        }


        private const double SYSTEM_SHAPE_SIZE = 18;
        private const double SYSTEM_SHAPE_OFFSET = SYSTEM_SHAPE_SIZE / 2;
        private const double SYSTEM_TEXT_TEXT_SIZE= 6;
        private const double SYSTEM_TEXT_X_OFFSET = 10;
        private const double SYSTEM_TEXT_Y_OFFSET = 2;
        private const double SYSTEM_REGION_TEXT_X_OFFSET = 5;
        private const double SYSTEM_REGION_TEXT_Y_OFFSET = SYSTEM_TEXT_Y_OFFSET + SYSTEM_TEXT_TEXT_SIZE + 2;

        private const int SYSTEM_Z_INDEX = 20;
        private const int SYSTEM_LINK_INDEX = 19;

        /// <summary>
        /// Initialise the control
        /// </summary>
        public void Init()
        {
            EM = EVEData.EveManager.GetInstance();
            SelectedSystem = string.Empty;

            DynamicMapElements = new List<UIElement>();

            CharacterDropDown.ItemsSource = EM.LocalCharacters;
            ActiveCharacter = null;

            RegionSelectCB.ItemsSource = EM.Regions;
            SelectRegion(MapConf.DefaultRegion);

            uiRefreshTimer = new System.Windows.Threading.DispatcherTimer();
            uiRefreshTimer.Tick += UiRefreshTimer_Tick; ;
            uiRefreshTimer.Interval = new TimeSpan(0, 0, 2);
            uiRefreshTimer.Start();

            ToolBoxCanvas.DataContext = this;

            PropertyChanged += MapObjectChanged;
        }

        /// <summary>
        /// UI Refresh Timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UiRefreshTimer_Tick(object sender, EventArgs e)
        {
            ReDrawMap(false);
        }

        public void SelectSystem(string name, bool changeRegion = false)
        {
            EVEData.System sys = EM.GetEveSystem(name);

            if(sys == null)
            {
                return;
            }

            if(changeRegion && !Region.IsSystemOnMap(name))
            {
                SelectRegion(sys.Region);
            }


            foreach (EVEData.MapSystem es in Region.MapSystems.Values.ToList())
            {
                if (es.Name == name)
                {
                    SystemDropDownAC.SelectedItem = es;
                    SelectedSystem = es.Name;
                    AddHighlightToSystem(name);

                    break;
                }
            }


            // now setup the anom data


            EVEData.AnomData system = ANOMManager.GetSystemAnomData(name);
            ANOMManager.ActiveSystem = system;

            ///AnomSigList.ItemsSource = system.Anoms.Values;


        }



        /// <summary>
        /// Select A Region
        /// </summary>
        /// <param name="regionName">Region to Select</param>
        public void SelectRegion(string regionName)
        {
            // check we havent selected the same system
            if(Region !=null && Region.Name == regionName)
            {
                return;
            }

            EM.UpdateIDsForMapRegion(regionName);

            // check its a valid system
            EVEData.MapRegion mr = EM.GetRegion(regionName);
            if(mr == null)
            {
                return;
            }

            // update the selected region
            Region = mr;
            

            List<EVEData.MapSystem> newList = Region.MapSystems.Values.ToList().OrderBy(o => o.Name).ToList();
            SystemDropDownAC.ItemsSource = newList;

            ReDrawMap(true);

            // select the item in the dropdown
            RegionSelectCB.SelectedItem = Region;
        }

        /// <summary>
{'fraction_non_alphanumeric': 0.07177306208347949, 'fraction_numerical': 0.00587513153279551, 'mean_word_length': 1.2896004818309577, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '619376', 'n_tokens_mistral': 6016, 'n_tokens_neox': 5511, 'n_words': 1202}