
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace PlcGroupInstall
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> keyValues = new Dictionary<string, string>();
            
            keyValues.Add("称重", "../Project.FLY.Weight/FLY.Weight.UI.Server/bin/Debug/Gage1");

            //for (int i = 0; i < args.Count(); i++)
            //    Console.WriteLine($"args[{i}]={args[i]}");
            //Console.ReadLine();
            //if (args.Count() < 2)
            //{
            //    Console.WriteLine("args error!!!!  eg: PlcGroupInstall.exe IBC ../Project.FLY.IBC/FLY.IBC.UI.Server/bin/Debug/Gage1");
            //    return;
            //}

            foreach (var kv in keyValues) 
            {
                PlcGroupInstallOne(kv.Key, kv.Value);
            }
            
        }


        /// <summary>
        /// 在plcgroupDirPath目录下找到全部 设备连接变量表_xxx文件夹，且里面有Generated/plcgroup.json 复制 到 targetPath目录
        /// </summary>
        /// <param name="plcgroupDirPath"></param>
        /// <param name="targetPath"></param>
        static void PlcGroupInstallOne(string plcgroupDirPath, string targetPath) 
        {
            string tempPath = Path.Combine(plcgroupDirPath, "plcgroups");
            if (Directory.Exists(tempPath))
                Directory.Delete(tempPath, true);

            DirectoryInfo srcDi = new DirectoryInfo(plcgroupDirPath);
            if (!srcDi.Exists) {
                //异常，直接抛出错误
                throw new Exception($"不存在 {plcgroupDirPath}");
            }
            foreach (var subDi in srcDi.GetDirectories("设备连接变量表_*")) {
                string plcgroupFilePath = Path.Combine(subDi.FullName, "Generated", "plcgroup.json");
                if (!File.Exists(plcgroupFilePath)) {
                    //异常，直接抛出错误
                    throw new Exception($"不存在 {plcgroupFilePath}");
                }

                //复制到目标位置
                string plcgroupTempDirPath = Path.Combine(tempPath, subDi.Name, "Generated");
                
                Directory.CreateDirectory(plcgroupTempDirPath);
                File.Copy(plcgroupFilePath, Path.Combine(plcgroupTempDirPath, "plcgroup.json"));
            }

            //复制到最终位置
            targetPath = Path.Combine(targetPath, "plcgroups");
            if(Directory.Exists(targetPath))
                Directory.Delete(targetPath,true );
            CopyDirectory(tempPath, targetPath);

            //删除临时文件夹
            Directory.Delete(tempPath,true);
        }


        static void CopyDirectory(string sourceDirPath, string SaveDirPath)
        {
            //如果指定的存储路径不存在，则创建该存储路径
            if (!Directory.Exists(SaveDirPath))
            {
                //创建
                Directory.CreateDirectory(SaveDirPath);
            }
            //获取源路径文件的名称
            string[] files = Directory.GetFiles(sourceDirPath);
            //遍历子文件夹的所有文件
            foreach (string file in files)
            {
                string pFilePath = Path.Combine(SaveDirPath, Path.GetFileName(file));
                File.Copy(file, pFilePath, true);
            }
            string[] dirs = Directory.GetDirectories(sourceDirPath);
            //递归，遍历文件夹
            foreach (string dir in dirs)
            {
                CopyDirectory(dir, Path.Combine(SaveDirPath,Path.GetFileName(dir)));
            }
        }


    }
    
}
