using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ProtobufCompiler : MonoBehaviour
{
    [MenuItem("Tool/程序/生成 Protobuf")]

    public static void Compile() {
        var rootDir = BuildUtils.CombineToFull(Application.dataPath, "app/Editor/ProtobufCompiler");
        var protoFileDir = BuildUtils.CombineToFull(rootDir, "protoFile");
        var outputFileDir = BuildUtils.CombineToFull(rootDir, "output");
        var destFileDir = BuildUtils.CombineToFull(Application.dataPath, "app/res/proto");
        var protoc = BuildUtils.CombineToFull(rootDir, "proto-3.17.3/bin/protoc.exe");

        if (!Directory.Exists(outputFileDir))
        {
            Directory.CreateDirectory(outputFileDir);
        }

        var p = new Process();
        p.StartInfo.FileName = protoc;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;

        var list = Directory.GetFiles(protoFileDir, "*.proto", SearchOption.AllDirectories);
        foreach (var filePath in list)
        {
            var fileDir = Path.GetDirectoryName(filePath);
            var fileName = Path.GetFileNameWithoutExtension(filePath);
            var fileExt = Path.GetExtension(filePath);

            var outputFilePath = Path.Combine(outputFileDir, fileName + ".pb");
            p.StartInfo.Arguments = "-I" + fileDir + " " + fileName + fileExt + " -o" + outputFilePath;
            p.Start();
            p.WaitForExit();
            if (p.ExitCode != 0)
            {
                UnityEngine.Debug.LogError(filePath + "协议生成失败");
                UnityEngine.Debug.LogError(p.StandardError.ReadToEnd());
            }
        }

        list = Directory.GetFiles(outputFileDir, "*.pb", SearchOption.AllDirectories);
        var count = 0;
        foreach (var filePath in list)
        {
            var fileName = Path.GetFileName(filePath) + ".bytes";
            File.Copy(filePath, BuildUtils.CombineToFull(destFileDir, fileName), true);
            count++;
        }

        UnityEngine.Debug.Log("已生成并拷贝" + count + "个协议文件到" + destFileDir);
        GenResLink(destFileDir, list);
    }

    public static void GenResLink(string destFileDir, string[] list)
    {
        destFileDir = destFileDir.Replace("\\", "/");

        string projectDir = Path.GetDirectoryName(Application.dataPath).Replace("\\", "/") + "/";
        string destFileRelativeDir = destFileDir.Replace(projectDir, "");
        string reslinkRelativePath = destFileRelativeDir + "/protoreslink.asset";

        var allFiles = new List<(string, string, System.Type)>();
        foreach (var item in list)
        {
            string key = Path.GetFileName(item);
            string relativePath = $"{destFileRelativeDir}/{key}.bytes";
            allFiles.Add((key, relativePath, null));
        }
        AutoReslink.genOne(reslinkRelativePath, allFiles);
        UnityEngine.Debug.Log("生成 reslink 成功");

    }
}
