using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Collections.ObjectModel;
using System.IO;

namespace MultiLayout.MainEdit
{
    /// <summary>
    /// Window_Load.xaml 的交互逻辑
    /// </summary>
    public partial class WdLoadFile : FLY.ControlLibrary.WindowBigClose
    {
        ObservableCollection<string> mList = new ObservableCollection<string>();

        public WdLoadFile()
        {
            InitializeComponent();
            Init();
        }

        public void Init()
        {

            this.listview_profile.ItemsSource = mList;

            //查找 layout/graphcustoms 下的全部文件名
            string dirPath = System.IO.Path.Combine(FlyLayout.BasePath, "graphcustoms");
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            if (!directoryInfo.Exists) {
                return;
            }

            var files = directoryInfo.GetFiles("*.json");
            foreach (var file in files) {
                mList.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name));
            }
        }

        private void button_read_Click(object sender, RoutedEventArgs e)
        {
            if (listview_profile.SelectedItem != null)
            {
                string strTipRebootForExecute = "需要重启，才能生效";
                if (FLY.ControlLibrary.MyMessageBox.Show(strTipRebootForExecute) == true)
                {
                    string filename = listview_profile.SelectedItem as string;
                    string filePath = System.IO.Path.Combine(FlyLayout.BasePath, "graphcustoms", filename + ".json");
                    string destPath = System.IO.Path.Combine(FlyLayout.BasePath, "graphcustom.json");
                    File.Copy(filePath, destPath, true);

                    System.Windows.Forms.Application.Restart();
                    Application.Current.Shutdown();
                }
                return;
            }
        }

        private void button_del_Click(object sender, RoutedEventArgs e)
        {
            if (listview_profile.SelectedItem != null)
            {
                string filename = listview_profile.SelectedItem as string;
                string filePath = System.IO.Path.Combine(FlyLayout.BasePath, "graphcustoms", filename + ".json");
                File.Delete(filePath);

                mList.Remove(filename);
                FLY.ControlLibrary.Window_Tip.Show("删除成功",
                    filename,
                    TimeSpan.FromSeconds(2));
            }
        }

        private void button_save_Click(object sender, RoutedEventArgs e)
        {
            WdSaveFile w = new WdSaveFile();
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            if (w.ShowDialog() == true) {
                if (mList.Contains(w.FileName)) {
                    if (FLY.ControlLibrary.MyMessageBox.Show("文件已经存在,是否覆盖？")!=true)
                    {
                        return;
                    }
                }
                string dirPath = System.IO.Path.Combine(FlyLayout.BasePath, "graphcustoms");
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);

                string filename = w.FileName;
                string destPath = System.IO.Path.Combine(FlyLayout.BasePath, "graphcustoms", filename + ".json");
                string srcPath = System.IO.Path.Combine(FlyLayout.BasePath, "graphcustom.json");
                File.Copy(srcPath, destPath, true);
                if (!mList.Contains(w.FileName))
                {
                    mList.Add(filename);
                }
                FLY.ControlLibrary.Window_Tip.Show("保存成功",
                    filename,
                    TimeSpan.FromSeconds(2));

            }
        }
    }
}
