using System.Linq;
using System;
using System.Collections.Generic;
using UnityEditor;

public class BuildTool
{
    public enum EBundleType : byte
    {
        None,
        FirstPack,
        ALL
    }

    public class BuildParams
    {
        public string outPath;
        //public string version;
        public string defineSymbol;
        public string buildType;
        //public int bundlesVersion;
        public bool isPatch;
        public bool aab = false;
        public EBundleType bundleType = EBundleType.FirstPack;
        public bool split = false;
    }

    private const string Param_Out_Path = "--out";
    //private const string Param_Version = "--ver";
    //private const string Param_Bundle_Version_Code = "--code";
    private const string Param_Define_Symbol = "--define-symbol";
    private const string Param_Patch = "--patch";
    private const string Param_Split = "--split";
    private const string Param_Aab = "--aab";
    private const string Param_Bundle_Type = "--bundle-type";
    private const string Param_Build_Type = "--build-type";

    public static void CIBuildIOS()
    {
        List<string> args = new List<string>(Environment.GetCommandLineArgs());

        BuildParams bparams = new BuildParams()
        {
            outPath = args[args.IndexOf(Param_Out_Path) + 1],
            //version = args[args.IndexOf(Param_Version) + 1],
            defineSymbol = args.IndexOf(Param_Define_Symbol) == -1 ? string.Empty : args[args.IndexOf(Param_Define_Symbol) + 1],
            buildType = args.IndexOf(Param_Build_Type) == -1 ? "New" : args[args.IndexOf(Param_Build_Type) + 1],
            //bundlesVersion = int.Parse(args[args.IndexOf(Param_Bundle_Version_Code) + 1]),
            isPatch = args.Contains(Param_Patch),
            split = args.Contains(Param_Split),
            aab = args.Contains(Param_Aab),
            bundleType = Enum.Parse<EBundleType>(args[args.IndexOf(Param_Bundle_Type) + 1])
        };

        BuildiOS(bparams);
    }

    [MenuItem("Tools/Build Android")]
    public static void BuildAndroid()
    {

        BuildParams bparams = new BuildParams()
        {
            outPath = "../Bin/tysdkTest.apk"
        };

        BuildiOS(bparams);
    }

    public static void CIBuildAndroid()
    {
        List<string> args = new List<string>(Environment.GetCommandLineArgs());

        BuildParams bparams = new BuildParams()
        {
            outPath = args[args.IndexOf(Param_Out_Path) + 1],
            //version = args[args.IndexOf(Param_Version) + 1],
            defineSymbol = args.IndexOf(Param_Define_Symbol) == -1 ? string.Empty : args[args.IndexOf(Param_Define_Symbol) + 1],
            buildType = args.IndexOf(Param_Build_Type) == -1 ? "New" : args[args.IndexOf(Param_Build_Type) + 1],
            //bundlesVersion = int.Parse(args[args.IndexOf(Param_Bundle_Version_Code) + 1]),
            isPatch = args.Contains(Param_Patch),
            split = args.Contains(Param_Split),
            aab = args.Contains(Param_Aab),
            bundleType = Enum.Parse<EBundleType>(args[args.IndexOf(Param_Bundle_Type) + 1])
        };

        BuildAndroid(bparams);
    }

    private static void BuildAndroid(BuildParams buildParams)
    {

        var buildPlayerOptions = new BuildPlayerOptions
        {
            scenes = EditorBuildSettings.scenes.Select(x => x.path).ToArray(),
            locationPathName = buildParams.outPath,

            target = BuildTarget.Android,
            options = BuildOptions.None,
        };

        var build_report = BuildPipeline.BuildPlayer(buildPlayerOptions);

        if (build_report?.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded)
        {
            UnityEngine.Debug.Log($"[Build Player Success] {System.IO.Path.GetFullPath(buildParams.outPath)}");
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
        }
        else
        {
            UnityEngine.Debug.LogError($"Build Player Failed: {build_report?.summary}");
        }
    }


    [MenuItem("Tools/Build iOS")]
    public static void BuildiOS()
    {

        BuildParams bparams = new BuildParams()
        {
            outPath = "../Bin"
        };

        BuildiOS(bparams);
    }

    private static void BuildiOS(BuildParams buildParams)
    {

        var buildPlayerOptions = new BuildPlayerOptions
        {
            scenes = EditorBuildSettings.scenes.Select(x => x.path).ToArray(),
            locationPathName = buildParams.outPath,

            target = BuildTarget.iOS,
            options = BuildOptions.None,
        };

        var build_report = BuildPipeline.BuildPlayer(buildPlayerOptions);

        if (build_report?.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded)
        {
            UnityEngine.Debug.Log($"[Build Player Success] {System.IO.Path.GetFullPath(buildParams.outPath)}");
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
        }
        else
        {
            UnityEngine.Debug.LogError($"Build Player Failed: {build_report?.summary}");
        }
    }
}
