using System;

using System.Threading.Tasks;

using Windows.ApplicationModel.Background;

using Windows.Storage;

using Windows.UI.Core;

using Windows.UI.Popups;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Media.Imaging;

using Windows.UI.Xaml.Navigation;



// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409



namespace Zebra_RFID_Mobile

{

    /// <summary>

    /// An empty page that can be used on its own or navigated to within a Frame.

    /// </summary>

    public sealed partial class MainPage : Page

    {

        private static bool initDone = false;

        public static MainPage Current;

        public static bool isApplicationWindowActive;

        public ReaderWatcher readerWatcher;

        private static bool showStatus = false;

        public MainPage()

        {

            this.InitializeComponent();

            // This is a static public property that allows downstream pages to get a handle to the MainPage instance

            // in order to call methods that are in this class.

            Current = this;

            if (readerWatcher == null)

            {

                readerWatcher = new ReaderWatcher();

            }

            if (!initDone)

            {

                // set Initial settings for the application

                Uri imageURI = new Uri("ms-appx:///Assets/title_dpo_disabled.png", UriKind.Absolute);

                BitmapImage dpoImage = new BitmapImage(imageURI);

                ReaderConfig.DPOImage.DPOImage = dpoImage;

                imageURI = new Uri("ms-appx:///Assets/ic_battery_0.png", UriKind.Absolute);

                ReaderConfig.batteryDPO.BatteryDPOImage = new BitmapImage(imageURI); ;



                ReaderConfig.StartStopButtonStatus.STARTSTATUS = "START";

                ReaderConfig.locateData.LocateStartStop = "START";

                ReaderConfig.locateData.LocateTagID_ON = true;

                ReaderConfig.batteryLevel.BatteryStatus = "Status: No active connection";

                ReaderConfig.StartStopButtonStatus.isBankListEnabled = true;



                loadStoredSettings();

                initDone = true;

            }

            Window.Current.Activated += new WindowActivatedEventHandler(OnWindowActivated);

            PopUpSettings.DataContext = ReaderConfig.SettingsStaus;

            textSettingsStatusLabel.DataContext = ReaderConfig.SettingsStaus;

        }



        private void loadStoredSettings()

        {

            ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

            Object value = localSettings.Values["autoReconnect"];

            if (value != null)

                ReaderConfig.connectionConfig.autoReconnect = (bool)value;

            value = localSettings.Values["readerAvailable"];

            if (value != null)

                ReaderConfig.connectionConfig.readerAvailable = (bool)value;

            value = localSettings.Values["readerConnection"];

            if (value != null)

                ReaderConfig.connectionConfig.readerConnection = (bool)value;

            value = localSettings.Values["readerBatteryStatus"];

            if (value != null)

                ReaderConfig.connectionConfig.readerBatteryStatus = (bool)value;

            value = localSettings.Values["exportData"];

            if (value != null)

                ReaderConfig.connectionConfig.exportData = (bool)value;



        }



        private void OnWindowActivated(object sender, WindowActivatedEventArgs e)

        {

            isApplicationWindowActive = (e.WindowActivationState != CoreWindowActivationState.Deactivated);

            ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;

            if (!showStatus)

                settings.Values["isApplicationWindowActive"] = isApplicationWindowActive;

        }



        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)

                return;

            rootFrame.BackStack.Clear();

        }



        private void Tapped_Inventory(object sender, RoutedEventArgs e)

        {

            this.Frame.Navigate(typeof(PageInventory));

        }

        private void Tapped_RapidRead(object sender, RoutedEventArgs e)

        {

            this.Frame.Navigate(typeof(PageRapidRead));

        }



        internal void NotifyUser(string v, object statusMessage)

        {

            //throw new NotImplementedException();

        }



        private void Tapped_Settings(object sender, RoutedEventArgs e)

        {

            this.Frame.Navigate(typeof(PageSettings));

        }

        private void Tapped_LocateTag(object sender, RoutedEventArgs e)

        {

            this.Frame.Navigate(typeof(PageLocateTag));

        }

        private void Tapped_Filters(object sender, RoutedEventArgs e)

        {

            this.Frame.Navigate(typeof(PageFilters));

        }

        private void Tapped_Access(object sender, RoutedEventArgs e)

        {

            this.Frame.Navigate(typeof(PageAccess));

        }

        private void Tapped_About(object sender, RoutedEventArgs e)

        {

            this.Frame.Navigate(typeof(PageAbout));

        }



        internal async void ShowStatus(string title, string status)

        {



            await Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>

            {

                try

                {

                    MessageDialog msgbox = new MessageDialog(status, title);

                    showStatus = true;

                    await msgbox.ShowAsync();

                    showStatus = false;

                }

                catch (UnauthorizedAccessException)

                {

                }

            });



        }

        private void ProgressRing_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)

        {

            if ((bool)e.NewValue == true)

                Flyout.ShowAttachedFlyout((FrameworkElement)sender);

            else

            {

                SettingsStatusFlyout.Hide();

            }

        }



        private async void FlyoutShown(object sender, object e)

        {

            await Task.Delay(1000);

            ReaderConfig.SettingsStaus.SettingsStatusShow = false;

        }

    }



    public enum NotifyType

    {

        StatusMessage,

        ErrorMessage

    };





}

