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

[InitializeOnLoad]
public static class ProjectAutoSetting
{
    static ProjectAutoSetting()
    {
        OnAutoSetting();
    }


    static void OnAutoSetting()
    {
        if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
        {
            OnAndroid();
        }
    }

    static void OnAndroid()
    {
        string path = "./KeyStore/android.json"; //自己的路径
        string jsonContent = File.ReadAllText(path); // 读取 JSON 文件内容
        if (!string.IsNullOrEmpty(jsonContent))
        {
            AndroidJson dynamicObject = JsonUtility.FromJson<AndroidJson>(jsonContent);
            PlayerSettings.Android.useCustomKeystore = true;
            if (dynamicObject != null)
            {
                PlayerSettings.Android.keystoreName = "./KeyStore/" + dynamicObject.keystoreName;
                PlayerSettings.Android.keyaliasName = dynamicObject.keyaliasName;
                PlayerSettings.keystorePass = Environment.GetEnvironmentVariable("BAMBOO_PW");
                PlayerSettings.keyaliasPass = Environment.GetEnvironmentVariable("BAMBOO_PW");
                PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, dynamicObject.applicationIdentifier);
                PlayerSettings.companyName = dynamicObject.companyName;
                PlayerSettings.productName = dynamicObject.productName;
            }
        }

        string outPath = "./" + PlayerSettings.productName + "_" + System.DateTime.Now.ToString("yyyyMMddHHmm");

        EditorUserBuildSettings.SetBuildLocation(BuildTarget.Android, outPath);
    }

    // static void OnIOS()
    // {
    //     PlayerSettings.applicationIdentifier = "aaa";
    //     PlayerSettings.bundleVersion = "1.9.115";
    //     PlayerSettings.iOS.applicationDisplayName = "aaa";
    //     PlayerSettings.iOS.targetOSVersionString = "8";
    //     PlayerSettings.iOS.buildNumber = System.DateTime.Now.ToString("yyyyMMddHHmm");
    //     PlayerSettings.iOS.sdkVersion = iOSSdkVersion.DeviceSDK;
    // }
    private class AndroidJson
    {
        public string keystoreName;
        public string keyaliasName;
        public string applicationIdentifier;
        public string companyName;
        public string productName;

    }
}
