using System;
using System.Collections.Generic;
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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MaterialIcons;
using F4SD_AdaptableIcon.Enums;
using C4IT.Logging;
using static C4IT.Logging.cLogManager;
using System.IO;
using System.Threading;

namespace F4SD_AdaptableIcon_Export
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private List<cAdvancedIconInformation> iconInformation = new List<cAdvancedIconInformation>();
        public MainWindow() => InitializeComponent();

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            InstantiateIconInformation();
        }

        private void InstantiateIconInformation()
        {

            List<cAdvancedIconInformation> IconInformation = new List<cAdvancedIconInformation>();

            foreach (enumInternIcons internIcon in Enum.GetValues(typeof(enumInternIcons)))
            {                
                cAdvancedIconInformation tempIconInformation = new cAdvancedIconInformation() { IsVisible = true };
                tempIconInformation.UpdateIconInformation(internIcon);
                IconInformation.Add(tempIconInformation);
            }

            foreach (MaterialIconType materialIcon in Enum.GetValues(typeof(MaterialIconType)))
            {
                cAdvancedIconInformation tempIconInformation = new cAdvancedIconInformation() { IsVisible = true };
                tempIconInformation.UpdateIconInformation(materialIcon);
                IconInformation.Add(tempIconInformation);
            }

            iconInformation = IconInformation;
            //IconListUC.IconInformation = IconInformation;
            //IconGridUC.IconInformation = IconInformation;
        }

        private void folderPathButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var openFileDlg = new System.Windows.Forms.OpenFileDialog();
                openFileDlg.FileName = folderPathTextBox.Text;
                openFileDlg.ValidateNames = false;
                openFileDlg.CheckFileExists = false;
                openFileDlg.CheckPathExists = true;
                openFileDlg.FileName = "select a folder.";
                var result = openFileDlg.ShowDialog();
                if (result is System.Windows.Forms.DialogResult.OK)
                {
                    var _path = openFileDlg.FileName;
                    _path = System.IO.Path.GetDirectoryName(_path);
                    if (System.IO.Directory.Exists(_path))
                        folderPathTextBox.Text = _path;
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
            
        }


        public void WriteToPng(UIElement element, string filename)
        {
            var rect = new Rect(element.RenderSize);
            var visual = new DrawingVisual();

            using (var dc = visual.RenderOpen())
            {
                dc.DrawRectangle(new VisualBrush(element), null, rect);
            }

            var bitmap = new RenderTargetBitmap(
                (int)rect.Width, (int)rect.Height, 96, 96, PixelFormats.Default);
            bitmap.Render(visual);

            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmap));

            string directoryPath = System.IO.Path.GetDirectoryName(filename);
            Directory.CreateDirectory(directoryPath);

            using (var file = File.OpenWrite(filename))
            {
                encoder.Save(file);
            }
        }

        private async void exportButton_Click(object sender, RoutedEventArgs e)
        {

            foreach (cAdvancedIconInformation icon in iconInformation)
            {
                exampleIcon.Visibility = Visibility.Collapsed;
                string strFolder = "";
                if (icon.InternIcon != null)
                {
                    exampleIcon.SelectedMaterialIcon = null;
                    exampleIcon.SelectedInternIcon = icon.InternIcon;
                    strFolder = @folderPathTextBox.Text + "\\" + txtSize.Text + "x" + txtSize.Text + "\\intern\\" + exampleIcon.SelectedInternIcon.ToString() + ".png";
                }
                else if (icon.MaterialIcon != null)
                {
                    exampleIcon.SelectedInternIcon = null;
                    exampleIcon.SelectedMaterialIcon = icon.MaterialIcon;
                    strFolder = @folderPathTextBox.Text + "\\" + txtSize.Text + "x" + txtSize.Text + "\\material\\" + exampleIcon.SelectedMaterialIcon.ToString() + ".png";
                }
                else
                    continue;

                exampleIcon.Visibility = Visibility.Visible;
                await Task.Delay(100);

                WriteToPng(this.exampleIcon, strFolder);
            }

        }

        private void pixSize_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            var size = (int)e.NewValue;
            if (txtSize != null)
                txtSize.Text = size.ToString();

            if (exampleIcon != null)
            {
                exampleIcon.IconWidth = size;
                exampleIcon.IconHeight = size;
            }
        }

        private void folderPathTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (folderPathTextBox.Text != String.Empty)
            {
                exportButton.IsEnabled = true;
            }
            else
            {
                exportButton.IsEnabled = false;
            }
        }
    }
}
