using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
//Kamerazugriff
using Microsoft.Devices;
//ISS
using System.IO.IsolatedStorage;
namespace SLCamera
{
    public partial class Einstellungen : PhoneApplicationPage
    {
        PhotoCamera kamera;

        public Einstellungen()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            kamera = new PhotoCamera();
            kamera.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(kamera_Initialized);
            VorschauBrush.SetSource(kamera);

        }

        void kamera_Initialized(object sender, CameraOperationCompletedEventArgs e)
        {
            List<string> blitze = new List<string>();
            if (kamera.IsFlashModeSupported(FlashMode.On))
                blitze.Add("An");
            if(kamera.IsFlashModeSupported(FlashMode.Off))
                blitze.Add("Aus");
            if(kamera.IsFlashModeSupported(FlashMode.RedEyeReduction))
                blitze.Add("Rote Augen Reduktion");
            if (kamera.IsFlashModeSupported(FlashMode.Auto))
                blitze.Add("Automatisch");
            this.Dispatcher.BeginInvoke(delegate()
                {
                    lpBlitz.ItemsSource = blitze;
                });

            List<string> aufloesungen = new List<string>();
            IEnumerable<Size> sAufloesungen =  kamera.AvailableResolutions;
            for (int i = 0; i < sAufloesungen.Count<Size>(); i++)
            {
                Size s = sAufloesungen.ElementAt<Size>(i);
                aufloesungen.Add(String.Format("{0}x{1}", s.Width, s.Height));
            }
            this.Dispatcher.BeginInvoke(delegate()
            {
                lpAufloesung.ItemsSource = aufloesungen;
            });

            string strBlitz = "";
            string strAufloesung = "";
            bool boSchnurrbart = false;
            try
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains("Blitz"))
                    strBlitz = (string)IsolatedStorageSettings.ApplicationSettings["Blitz"];
                if (IsolatedStorageSettings.ApplicationSettings.Contains("Aufloesung"))
                    strAufloesung = (string)IsolatedStorageSettings.ApplicationSettings["Aufloesung"];
                if (IsolatedStorageSettings.ApplicationSettings.Contains("Schnurrbart"))
                    boSchnurrbart = (bool)IsolatedStorageSettings.ApplicationSettings["Schnurrbart"];
            }
            catch (Exception ex)
            {
            }

            this.Dispatcher.BeginInvoke(delegate()
            {
                if(strBlitz.Length > 0)
                    lpBlitz.SelectedItem = strBlitz;
                if (strAufloesung.Length > 0)
                    lpAufloesung.SelectedItem = strAufloesung;
                checkBoxSchnurrbart.IsChecked = boSchnurrbart;
            });
        }

        private void lpBlitz_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems != null && e.RemovedItems.Count > 0)
            {
                if (this.lpBlitz.SelectedItem != null)
                {
                    IsolatedStorageSettings.ApplicationSettings["Blitz"] = this.lpBlitz.SelectedItem;
                }
            }
        }

        private void lpAufloesung_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems != null && e.RemovedItems.Count > 0)
            {
                if (this.lpAufloesung.SelectedItem != null)
                {
                    IsolatedStorageSettings.ApplicationSettings["Aufloesung"] = this.lpAufloesung.SelectedItem;
                }
            }
        }

        private void checkBoxSchnurrbart_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["Schnurrbart"] = true;
        }

        private void checkBoxSchnurrbart_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings["Schnurrbart"] = false;
        }
    }
}
