using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WpfControlsAndAPIs {
    public class MyDoubleConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            // Convert the double to an int.
            double v = (double)value;
            return (int)v;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            // You won't worry about "two-way" bindings here, so just return the value.
            return value;
        }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        DataTable dataTable = null;
        public MainWindow() {
            InitializeComponent();
            // Be in Ink mode by default.
            this.MyInkCanvas.EditingMode = InkCanvasEditingMode.Ink;
            this.inkRadio.IsChecked = true;
            this.comboColors.SelectedIndex = 0;
            SetBindings();
            
        }

        private void GenerateDG(object sender, RoutedEventArgs e) {
            if (dataTable != null)
                dataTable.Clear();

            dataTable = new DataTable();

            dataTable.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
            dataTable.Columns.Add(new System.Data.DataColumn("Position", typeof(string)));
            dataTable.Columns.Add(new System.Data.DataColumn("Office", typeof(string)));
            dataTable.Columns.Add(new System.Data.DataColumn("Age", typeof(int)));
            dataTable.Columns.Add(new System.Data.DataColumn("StartDate", typeof(DateTime)));
            dataTable.Columns.Add(new System.Data.DataColumn("Salary", typeof(int)));

            dataTable.Rows.Add("Tiger Nixon", "System Architect", "Edinburgh", 61, new DateTime(2011, 04, 25), 320000);
            dataTable.Rows.Add("Sonya Frost", "Software Engineer", "Edinburgh", 23, new DateTime(2008, 08, 13), 103600);
            dataTable.Rows.Add("Jena Gaines", "Office Manager", "London", 30, new DateTime(2008, 12, 19), 90560);
            dataTable.Rows.Add("Quinn Flynn", "Support Lead", "Edinburgh", 22, new DateTime(2013, 03, 03), 342000);
            dataTable.Rows.Add("Charde Marshall", "Regional Director", "San Francisco	", 36, new DateTime(2008, 10, 16), 470060);
            dataTable.Rows.Add("Haley Kennedy", "Senior Marketing Designer", "London", 43, new DateTime(2012, 12, 18), 313500);

            myDGV.DataContext = dataTable.DefaultView;
        }

        private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {
           // if (e.PropertyType == typeof(System.DateTime))
           //     (e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
        }

        private void SaveDG(object sender, RoutedEventArgs e) {
            dataTable.TableName = "Tablica danych";
            dataTable.WriteXml("dane.xml");
        }

        private void LoadDG(object sender, RoutedEventArgs e) {
            myDGV.DataContext = null;
            myDGV.AutoGenerateColumns = true;
            XmlReader xmlFile = XmlReader.Create("dane.xml", new XmlReaderSettings());
            DataSet data = new DataSet();
            data.ReadXml(xmlFile);
            xmlFile.Close();

            dataTable = data.Tables[0];
            myDGV.DataContext = dataTable.DefaultView;
        }


        private void SetBindings() {
            Binding b = new Binding {  // Create a Binding object.
                Converter = new MyDoubleConverter(), // Register the converter, source, and path.
                Source = this.mySB,
                Path = new PropertyPath("Value")
            };
            // Specify the dependency property!
            this.labelSBThumb.SetBinding(Label.ContentProperty, b);
        }

        private void RadioButtonClicked(object sender, RoutedEventArgs e) {
            // Based on which button sent the event, place the InkCanvas in a unique mode of operation.
            this.MyInkCanvas.EditingMode = (sender as RadioButton)?.Content.ToString() switch {
                // These strings must be the same as the Content values for each RadioButton.
                "Ink Mode!" => InkCanvasEditingMode.Ink,
                "Erase Mode!" => InkCanvasEditingMode.EraseByStroke,
                "Select Mode!" => InkCanvasEditingMode.Select,
                _ => this.MyInkCanvas.EditingMode
            };
        }
        private void ColorChanged(object sender, SelectionChangedEventArgs e) {
            string colorToUse = (this.comboColors.SelectedItem as StackPanel).Tag.ToString();
            this.MyInkCanvas.DefaultDrawingAttributes.Color = (Color)ColorConverter.ConvertFromString(colorToUse);
        }

        private void SaveData(object sender, RoutedEventArgs e) {
            // Save all data on the InkCanvas to a local file.
            using (FileStream fs = new FileStream("StrokeData.bin", FileMode.Create)) {
                this.MyInkCanvas.Strokes.Save(fs);
            }
        }
        private void LoadData(object sender, RoutedEventArgs e) {
            using (FileStream fs = new FileStream("StrokeData.bin", FileMode.Open, FileAccess.Read)) {
                StrokeCollection strokes = new StrokeCollection(fs);
                this.MyInkCanvas.Strokes = strokes;
            }
        }
        private void Clear(object sender, RoutedEventArgs e) {
            this.MyInkCanvas.Strokes.Clear();
        }
    }
}
