using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

using KoloryWPF.ModelWidoku;

namespace KoloryWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Color KolorProstokąta
        {
            get
            {
                return (rectangle.Fill as SolidColorBrush).Color;
            }
            set
            {
                (rectangle.Fill as SolidColorBrush).Color = value;
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            Color kolor = Model.Ustawienia.Czytaj().ToColor();
            rectangle.Fill = new SolidColorBrush(kolor);
            sliderR.Value = kolor.R;
            sliderG.Value = kolor.G;
            sliderB.Value = kolor.B;
        }

        private void sliderR_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            Color kolor = Color.FromRgb(
                (byte)(sliderR.Value),
                (byte)(sliderG.Value),
                (byte)(sliderB.Value));
            KolorProstokąta = kolor;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape) Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Model.Ustawienia.Zapisz(
                new Model.Kolor(KolorProstokąta.R, KolorProstokąta.G, KolorProstokąta.B));
        }
    }
}
