﻿using Microsoft.VisualBasic;
using Microsoft.WindowsAPICodePack.Dialogs;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
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.Shapes;

namespace CalcsGenerator
{
    /// <summary>
    /// Логика взаимодействия для ConvertWindow.xaml
    /// </summary>
    public partial class ConvertWindow : Window
    {
        Project proj = new Project();

        public ConvertWindow()
        {
            InitializeComponent();
            proj.Name = "Восстановленный проект " + DateTime.Now;
            proj.Tabs = new List<Tab>();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                FileStream fs = new FileStream(dialog.FileName, FileMode.OpenOrCreate);

                CalcSaver saver = (CalcSaver)formatter.Deserialize(fs);
                saver.RestoreTabs(AddNewTab);

                fs.Close();
            }

            CommonSaveFileDialog sdialog = new CommonSaveFileDialog();
            if(dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                FileStream fs = new FileStream(dialog.FileName, FileMode.OpenOrCreate);
                StreamWriter writer = new StreamWriter(fs);
                writer.Write(JsonConvert.SerializeObject(proj));
                writer.Flush();
                writer.Close();
                fs.Close();
            }
        }

        private void AddNewTab(string title, PriceListItem[] initlist)
        {
            Tab tab = new Tab();
            tab.TabRecords = new List<TabRecord>();
            tab.Name = title;
            foreach(var item in initlist)
            {
                if (item.Type == "%")
                {
                    if(item.Name.IndexOf("расходы ") > -1)
                    {
                        tab.WorkCharge = item.Count;
                    }
                    else
                    {
                        tab.PartsCharge = item.Count;
                    }
                }

                else
                {
                    TabRecord record = new TabRecord();
                    record.Name = item.Name;
                    record.Nds = item.Nds;
                    record.Usn = item.Usn;
                    record.Real = (int)item.Real;
                    record.Count = item.Count;
                    record.Charge = (int)item.Сharge;
                    record.Type = item.Type;
                    tab.TabRecords.Add(record);
                }
            }
            proj.Tabs.Add(tab);
        }
    }
}
