using PlantBox.Client.Resources;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PlantBox.Client.Forms.Settings
{
	[XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class SettingsPage : ContentPage
	{
		public SettingsPage()
		{
			InitializeComponent();

            // Save on close
            Disappearing += async (s, e) => await App.Settings.SaveAsync();

            // Can't apply style to cells, so...
            ApplyStyle();
		}

        private void ApplyStyle()
        {
            foreach (var cell in table.Root.SelectMany(x => x))
            {
                if (cell is TextCell textCell)
                {
                    if (Device.RuntimePlatform == Device.UWP)
                    {
                        textCell.TextColor = Color.Accent;
                    }
                }
                else if (cell is SwitchCell switchCell)
                {
                    switchCell.Tapped += (s, e) => (s as SwitchCell).On = !(s as SwitchCell).On;
                }
            }
        }

        private async void Language_Tapped(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new LanguageSelectorPage());
        }
    }
}