/// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            FootballMatch DataItem = await DataSource.GetItemAsync((int)e.NavigationParameter);
            ItemId = (int)e.NavigationParameter;
            if (DataItem.incident == null || DataItem.incident.deleted == true) 
            {
                IncidentPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                AddIncidentButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
                
            }
            else
            {
                IncidentPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
                AddIncidentButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                AcceptButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
             if (DataItem.injury == null || DataItem.injury.deleted == true) 
            {
                InjuryPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                AddInjuryButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                AddInjuryButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                InjuryPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
                AcceptButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
             this.DefaultViewModel["Incidents"] = DataItem;

}
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {

            try
            {
                await DataSource.RefreshAccessToken();
            }
            catch (Exception ex)
            {

            }
            FootballMatch item = await DataSource.GetItemAsync((int)e.NavigationParameter);
            prevRoundId = item.round_id;
            this.DefaultViewModel["Item"] = item;
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            try
            {
                loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Visible;
                synchProgressRing.IsActive = true;
                var DataGroups = await DataSource.GetAdditionAsync((int)e.NavigationParameter);
                if (DataGroups == null || DataGroups.objectStatus == (int)DataSource.status.Deleted)
                {
                    CreateNew.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    dataPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DeleteButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    BackButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else
                {
                    CreateNew.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    dataPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    DeleteButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    BackButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
                activId = (int)e.NavigationParameter;
                if (DataGroups != null)
                {
                    StartClock.Time = new TimeSpan(DataGroups.starthour, DataGroups.startminute, 0);
                    EndClock.Time = new TimeSpan(DataGroups.endhour, DataGroups.endminute, 0);
                    this.DefaultViewModel["Addition"] = DataGroups;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                synchProgressRing.IsActive = false;
            }
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            try
            {
                loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Visible;
                synchProgressRing.IsActive = true;
                int tournamentId = (int)e.NavigationParameter;
                var TournamentData = await DataSource.GetTournamentAsync(tournamentId);
                this.DefaultViewModel["Tournament"] = TournamentData;
                this.DefaultViewModel["userlogin"] = DataSource.login;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                synchProgressRing.IsActive = false;
            }

        }
 /// <summary>
 /// Populates the page with content passed during navigation.  Any saved state is also
 /// provided when recreating a page from a prior session.
 /// </summary>
 /// <param name="sender">
 /// The source of the event; typically <see cref="NavigationHelper"/>
 /// </param>
 /// <param name="e">Event data that provides both the navigation parameter passed to
 /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
 /// a dictionary of state preserved by this page during an earlier
 /// session.  The state will be null the first time a page is visited.</param>
 private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
 {
     try
     {
         loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Visible;
         synchProgressRing.IsActive = true;
         var TournamentsData = await DataSource.GetTournamentsAsync();
         this.DefaultViewModel["Tournaments"] = TournamentsData;
         this.DefaultViewModel["userlogin"] = DataSource.login;
     }
     catch(Exception ex)
     {
         var dialog = new MessageDialog("Ошибка при загрузке данных, повторите позже или обратитесь к администратору");
         dialog.ShowAsync();
         Frame.Navigate(typeof(AuthorizationPage));
     }
     finally
     {
         loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
         synchProgressRing.IsActive = false;
         //CommandBarApp.Visibility = Windows.UI.Xaml.Visibility.Visible;
     }
 }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            
            try
            {
                loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Visible;
                synchProgressRing.IsActive = true;
                var Data = await DataSource.GetItemsAsync((int)e.NavigationParameter);
                Round RoundData = await DataSource.GetRoundAsync((int)e.NavigationParameter);
                this.DefaultViewModel["ActualMatches"] = Data.Where((status) => status.actual.Equals(true));
                this.DefaultViewModel["PastMatches"] = Data.Where((status) => status.actual.Equals(false));
                this.DefaultViewModel["RoundInfo"] = RoundData;
                this.DefaultViewModel["userlogin"] = DataSource.login;
                PrevItemId = ((Round)this.DefaultViewModel["RoundInfo"]).tournament_id;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                synchProgressRing.IsActive = false;
            }
            //var roundMatches = _DataSource.Tournaments.SelectMany(tournament => tournament.RoundsList).Where((round) => round.id.Equals(uniqueId));
            //var matches = roundMatches.SelectMany(round => round.Items);//_DataSource.Tournaments.SelectMany(tournament => tournament.RoundsList).SelectMany(round => round.Items);
            //if (matches.Count() > 0) return matches;
            
            // TODO: Create an appropriate data model for your problem domain to replace the sample data
        //    var DataGroups = await DataSource.GetGroupsAsync();
       //     this.DefaultViewModel["Groups"] = DataGroups;
        //    if (GeneralApplicationData.accessToken == null)
       //     {
        //        loginButton.Content = "Войти";
       //        // loginButton.Click += LoginButton_Click;
        //    }
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            //try
            TeamComboBox.SelectedIndex = 0;
            activId = (int)e.NavigationParameter;

            await LoadState();


        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            //try
            //DeleteButtonEd.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            activId = (int)e.NavigationParameter;
            FootballMatch Data = await DataSource.GetItemAsync((int)e.NavigationParameter);
            dataGroupsMatchPlayerTeam1 = new ObservableCollection<DataGroupMatchPlayers>();
            dataGroupsMatchPlayerTeam2 = new ObservableCollection<DataGroupMatchPlayers>();
            DataGroupMatchPlayers dp = null;
            loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            foreach (Player pl in Data.team1_players)
            {
                dp = new DataGroupMatchPlayers(Data, Data.team1, pl);
                foreach (MatchPlayer mp in Data.matchPlayers)
                {
                    if (dp.player.id == mp.Player_idValue)
                    {
                        dp.matchPlayer = mp;
                        if (mp.objectStatus != (int)DataSource.status.Deleted && mp.objectStatus != (int)DataSource.status.needDelete) dp.showProperties = true;
                    }
                }
                if (dp.matchPlayer.IdValue == -1)
                {
                    dp.matchPlayer.Match_idValue = Data.id;
                    dp.matchPlayer.Player_idValue = pl.id;
                    dp.matchPlayer.Team_idValue = Data.team1.id;
                    dp.matchPlayer.TeamsheetValue = 0;
                }
                dataGroupsMatchPlayerTeam1.Add(dp);
            }
            foreach (Player pl in Data.team2_players)
            {
                dp = new DataGroupMatchPlayers(Data, Data.team2, pl);
                foreach (MatchPlayer mp in Data.matchPlayers)
                {
                    if (dp.player.id == mp.Player_idValue)
                    {
                        dp.matchPlayer = mp;
                        if (mp.objectStatus != (int)DataSource.status.Deleted && mp.objectStatus != (int)DataSource.status.needDelete) dp.showProperties = true;
                    }
                }
                if (dp.matchPlayer.IdValue == -1)
                {
                    dp.matchPlayer.Match_idValue = Data.id;
                    dp.matchPlayer.Player_idValue = pl.id;
                    dp.matchPlayer.Team_idValue = Data.team2.id;
                    dp.matchPlayer.TeamsheetValue = 0;
                }
                dataGroupsMatchPlayerTeam2.Add(dp);
            }

            this.DefaultViewModel["Match"] = Data;
            if (dataGroupsMatchPlayerTeam1.Count() > 0)
            {
                this.DefaultViewModel["Team1MatchPlayers"] = dataGroupsMatchPlayerTeam1;
            }
            if (dataGroupsMatchPlayerTeam2.Count() > 0)
            {
                this.DefaultViewModel["Team2MatchPlayers"] = dataGroupsMatchPlayerTeam2;
            }
            TeamComboBox.SelectedIndex = 0;

        }
 /// <summary>
 /// Populates the page with content passed during navigation.  Any saved state is also
 /// provided when recreating a page from a prior session.
 /// </summary>
 /// <param name="sender">
 /// The source of the event; typically <see cref="NavigationHelper"/>
 /// </param>
 /// <param name="e">Event data that provides both the navigation parameter passed to
 /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
 /// a dictionary of state preserved by this page during an earlier
 /// session.  The state will be null the first time a page is visited.</param>
 private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
 {
 }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            try
            {
                loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Visible;
                synchProgressRing.IsActive = true;
                FootballMatch Data = await DataSource.GetItemAsync((int)e.NavigationParameter);
                
                activId = (int)e.NavigationParameter;
                if (Data != null)
                {
                    this.DefaultViewModel["MatchInfo"] = Data;
                }
                ObservableCollection<int> scoreValue = new ObservableCollection<int>();
                for (int i = 0; i <= 40; i++)
                {
                    scoreValue.Add(i);
                }
                this.DefaultViewModel["scoreValue"] = scoreValue;
                this.DefaultViewModel["_timePart"] = new ConcTime(Data.start_at, Data);
                //Goals1ComboBoxEd.ItemsSource = this.DefaultViewModel["scoreValue"];
                //Goals2ComboBoxEd.ItemsSource = this.DefaultViewModel["scoreValue"];
                //penalty1ComboBoxEd.ItemsSource = this.DefaultViewModel["scoreValue"];
                //penalty2ComboBoxEd.ItemsSource = this.DefaultViewModel["scoreValue"];
                //dpFromDate.Date = DateTime.Parse(Data.start_at);
                ///dpFromDate.
                //ObservableColletcion scoreValue = new ObservableColletcion;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                loadingRectangle.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                synchProgressRing.IsActive = false;
            }
        }