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;
            
            //查找 default/layout.default 下的全部文件夹名
            string dirPath = System.IO.Path.Combine("default", "layout.default");
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            if (!directoryInfo.Exists) {
                return;
            }

            var directoryInfos = directoryInfo.GetDirectories();

            foreach (var dirInfo in directoryInfos) {
                //检查这个文件夹是否正确
                string filePath = System.IO.Path.Combine(dirInfo.FullName, "graphcustom.json");
                if (File.Exists(filePath)) {
                    //正确
                    mList.Add(dirInfo.Name);
                }
            }
        }

        private void button_read_Click(object sender, RoutedEventArgs e)
        {
            if (listview_profile.SelectedItem == null)
                return;

            string strTipRebootForExecute = "需要重启,才能生效";
            if (FLY.ControlLibrary.MyMessageBox.Show(strTipRebootForExecute) != true)
                return;

            string dirname = listview_profile.SelectedItem as string;

            string srcDirPath = System.IO.Path.Combine("default", "layout.default", dirname);

            //把 srcDirPath 下的全部文件 复制到 FlyLayout.BasePath 下
            DirectoryInfo srcDirInfo = new DirectoryInfo(srcDirPath);

            var fileInfos = srcDirInfo.GetFiles();

            foreach (var fileInfo in fileInfos)
            {
                string filePath = fileInfo.FullName;
                string destPath = System.IO.Path.Combine(FlyLayout.BasePath, fileInfo.Name);
                File.Copy(filePath, destPath, true);
            }
            System.Windows.Forms.Application.Restart();
            Application.Current.Shutdown();
                
            
        }

        private void button_del_Click(object sender, RoutedEventArgs e)
        {
            if (listview_profile.SelectedItem == null)
                return;

           string dirname = listview_profile.SelectedItem as string;
            if (FLY.ControlLibrary.MyMessageBox.Show($"将要删除 {dirname} ,是否确定?") != true)
                return;

            string srcDirPath = System.IO.Path.Combine("default", "layout.default", dirname);
            Directory.Delete(srcDirPath, true);

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

        private void button_save_Click(object sender, RoutedEventArgs e)
        {
            WdSaveFile w = new WdSaveFile();

            if (listview_profile.SelectedItem != null)
                w.FileName = listview_profile.SelectedItem as string;

            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            if (w.ShowDialog() != true)
                return;

            if (mList.Contains(w.FileName)) {
                if (FLY.ControlLibrary.MyMessageBox.Show("布局文件已经存在,是否覆盖?")!=true)
                    return;
                    
            }

            string dirname = w.FileName;
            string destDirPath = System.IO.Path.Combine("default", "layout.default", dirname);
            string srcDirPath = FlyLayout.BasePath;

            if (!Directory.Exists(destDirPath))
                Directory.CreateDirectory(destDirPath);


            //把 srcDirPath 下的全部文件 复制到 destDirPath 下
            DirectoryInfo srcDirInfo = new DirectoryInfo(srcDirPath);

            var fileInfos = srcDirInfo.GetFiles();

            foreach (var fileInfo in fileInfos)
            {
                string filePath = fileInfo.FullName;
                string destPath = System.IO.Path.Combine(destDirPath, fileInfo.Name);
                File.Copy(filePath, destPath, true);
            }


            if (!mList.Contains(w.FileName))
                mList.Add(w.FileName);
            
            FLY.ControlLibrary.Window_Tip.Show("保存成功",
                w.FileName,
                TimeSpan.FromSeconds(2));
        }
    }
}