using Avalonia.Controls;
using Avalonia.Threading;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using static DaireApplication.Views.MainWindow;

namespace DaireApplication.ViewModels
{
    public class Error
    {
        public enum GridCondition
        {
            GridFrequencyHigh,
            GridFrequencyLow,
            GridVACHigh,
            GridVACLow,
            NoExternalPower,
            MissingPhase,
            PhaseSequence,
            LoadDisconnection,
            MotorDisconnection,
            NoBoardCom,
            NoInternetAccess,
            ComPort1,
            ComPort2,
            HiCurrNeut,
            HiCurrMot1,
            HiCurrMot2,
        }
        public DateTime errorDate { get; set; }
        public GridCondition Condition { get; set; }
        public bool isShowen { get; set; } = false;
        public bool isDeleted { get; set; } = false;

        public string GetDisplayNames(GridCondition condition)
        {
            return condition switch
            {
                GridCondition.GridFrequencyHigh => "Grid Frequency High",
                GridCondition.GridFrequencyLow => "Grid Frequency Low",
                GridCondition.GridVACHigh => "Grid VAC High",
                GridCondition.GridVACLow => "Grid VAC Low",
                GridCondition.NoExternalPower => "No External Power",
                GridCondition.MissingPhase => "Missing Phase",
                GridCondition.PhaseSequence => "Phase Sequence",
                GridCondition.LoadDisconnection => "Load Disconnection",
                GridCondition.MotorDisconnection => "Motor Disconnection",
                GridCondition.NoBoardCom => "Communication Timeout",
                GridCondition.NoInternetAccess => "No InternetAccess",
                GridCondition.ComPort1 => "Com Port1",
                GridCondition.ComPort2 => "Com Port2",
                GridCondition.HiCurrNeut=> "Hi Curr Neut",
                GridCondition.HiCurrMot1=> "Hi Curr Mot1",
                GridCondition.HiCurrMot2=> "Hi Curr Mot2",
                _ => string.Empty
            };


        }
        public static bool IsInternetAvailable()
        {
            try
            {
                var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

                foreach (var networkInterface in networkInterfaces)
                {
                    if (networkInterface.OperationalStatus == OperationalStatus.Up)
                    {
                        using (var ping = new Ping())
                        {
                            var reply = ping.Send("8.8.8.8", 3000); // Pinging Google DNS server with a timeout of 3000ms
                            if (reply != null && reply.Status == IPStatus.Success)
                            {
                                return true; // Internet is available
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                return false; // Return false if there is any exception (e.g., no network interface found, or errors during checking)
            }

            return false; // Return false if no network interfaces are available or no successful ping
        }
    }
}
