using Avalonia.Media.Imaging;
using Avalonia.Threading;
using DaireApplication.ViewModels;
using DaireApplication.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DaireApplication.Loops
{
    public class CheckInterNetLoop
    {
        public static async void CheckInterNet(MainWindow _mainWindow)
        {
            string textToDelete = "";

            while (true)
            {
                if (!Error.IsInternetAvailable())
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        if (_mainWindow.ContentArea.Content is Home  || _mainWindow.ContentArea.Content is Admin)
                        {
                            if (!_mainWindow.nowifiLogo.IsVisible)
                            {
                                _mainWindow.wifiLogo.IsVisible = false;
                                _mainWindow.nowifiLogo.IsVisible = true;
                            }
                        }
                        else
                        {
                            _mainWindow.wifiLogo.IsVisible = false;
                            _mainWindow.nowifiLogo.IsVisible = false;
                        }
                        //if (!_mainWindow.warningMsg.Text.Contains("No Internet Access"))
                        //{
                        //    _mainWindow.warningMsg.Text += "\nNo Internet Access";
                        //    _mainWindow.warningLogo.IsVisible = true;

                        //}

                    });

                }
                else
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        if (_mainWindow.ContentArea.Content is Home || _mainWindow.ContentArea.Content is Admin)
                        {
                            if (!_mainWindow.wifiLogo.IsVisible)
                            {
                                _mainWindow.nowifiLogo.IsVisible = false;
                                _mainWindow.wifiLogo.IsVisible = true;
                            }
                        }
                        else
                        {
                            _mainWindow.wifiLogo.IsVisible = false;
                            _mainWindow.nowifiLogo.IsVisible = false;
                        }
                        //if (_mainWindow.warningMsg.Text.Contains("No Internet Access"))
                        //{
                        //    _mainWindow.warningMsg.Text=_mainWindow.warningMsg.Text.Replace("\nNo Internet Access", "");
                        //}
                    });

                }
                if (!string.IsNullOrEmpty(_mainWindow.warningMessage))
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        if (!_mainWindow.warningMsg.Text.Contains($"\n-{_mainWindow.warningMessage}"))
                        {
                            textToDelete = _mainWindow.warningMessage;
                            _mainWindow.warningMsg.Text += $"\n-{_mainWindow.warningMessage}";
                            _mainWindow.warningLogo.IsVisible = true;

                        }

                    });
                }
                else
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        if (_mainWindow.warningMsg.Text.Contains($"-{textToDelete}"))
                        {
                            _mainWindow.warningMsg.Text= _mainWindow.warningMsg.Text.Replace($"\n-{textToDelete}", "");
                        }
                       
                    });
                }
                if ( string.IsNullOrEmpty(_mainWindow.warningMessage))
                {
                    Dispatcher.UIThread.Post(() =>
                    {
                        _mainWindow.warningLogo.IsVisible = false;

                    });    
                }
                Thread.Sleep(10);
            }
        }

    }
}
