//
//  AppLovinSettings.cs
//  AppLovin MAX Unity Plugin
//
//  Created by Santosh Bagadi on 1/27/20.
//  Copyright © 2019 AppLovin. All rights reserved.
//
using AppLovinMax.Scripts.IntegrationManager.Editor;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;
/// 
/// A  representing the AppLovin Settings that can be set in the Integration Manager Window.
///
/// The scriptable object asset is created with the name AppLovinSettings.asset and is placed under the directory Assets/MaxSdk/Resources.
///
/// NOTE: Not name spacing this class since it is reflected upon by the Google adapter and will break compatibility.
/// 
public class AppLovinSettings : ScriptableObject
{
    private const string SettingsExportPath = "MaxSdk/Resources/AppLovinSettings.asset";
    private static AppLovinSettings _instance;
    [SerializeField] private bool qualityServiceEnabled = true;
    [SerializeField] private string sdkKey;
    [SerializeField] private string customGradleVersionUrl;
    [SerializeField] private string customGradleToolsVersion;
    [SerializeField] private string adMobAndroidAppId = string.Empty;
    [SerializeField] private string adMobIosAppId = string.Empty;
    /// 
    /// An instance of AppLovin Setting.
    /// 
    public static AppLovinSettings Instance
    {
        get
        {
            if (_instance == null)
            {
                // Check for an existing AppLovinSettings somewhere in the project
                var guids = AssetDatabase.FindAssets("AppLovinSettings t:ScriptableObject");
                if (guids.Length > 1)
                {
                    MaxSdkLogger.UserWarning("Multiple AppLovinSettings found. This may cause unexpected results.");
                }
                if (guids.Length != 0)
                {
                    var path = AssetDatabase.GUIDToAssetPath(guids[0]);
                    _instance = AssetDatabase.LoadAssetAtPath(path);
                    return _instance;
                }
                // If there is no existing AppLovinSettings asset, create one in the default location
                string settingsFilePath;
                // The settings file should be under the Assets/ folder so that it can be version controlled and cannot be overriden when updating.
                // If the plugin is outside the Assets folder, create the settings asset at the default location.
                if (AppLovinIntegrationManager.IsPluginInPackageManager)
                {
                    // Note: Can't use absolute path when calling `CreateAsset`. Should use relative path to Assets/ directory.
                    settingsFilePath = Path.Combine("Assets", SettingsExportPath);
                    var maxSdkDir = Path.Combine(Application.dataPath, "MaxSdk");
                    if (!Directory.Exists(maxSdkDir))
                    {
                        Directory.CreateDirectory(maxSdkDir);
                    }
                }
                else
                {
                    settingsFilePath = Path.Combine(AppLovinIntegrationManager.PluginParentDirectory, SettingsExportPath);
                }
                var settingsDir = Path.GetDirectoryName(settingsFilePath);
                if (!Directory.Exists(settingsDir))
                {
                    Directory.CreateDirectory(settingsDir);
                }
                // On script reload AssetDatabase.FindAssets() can fail and will overwrite AppLovinSettings without this check
                if (!File.Exists(settingsFilePath))
                {
                    _instance = CreateInstance();
                    AssetDatabase.CreateAsset(_instance, settingsFilePath);
                    MaxSdkLogger.D("Creating new AppLovinSettings asset at path: " + settingsFilePath);
                }
            }
            return _instance;
        }
    }
    /// 
    /// Whether or not to install Quality Service plugin.
    /// 
    public bool QualityServiceEnabled
    {
        get { return Instance.qualityServiceEnabled; }
        set { Instance.qualityServiceEnabled = value; }
    }
    /// 
    /// AppLovin SDK Key.
    /// 
    public string SdkKey
    {
        get { return Instance.sdkKey; }
        set { Instance.sdkKey = value; }
    }
    /// 
    /// A URL to set the distributionUrl in the gradle-wrapper.properties file (ex: https\://services.gradle.org/distributions/gradle-6.9.3-bin.zip)
    /// 
    public string CustomGradleVersionUrl
    {
        get { return Instance.customGradleVersionUrl;  }
        set { Instance.customGradleVersionUrl = value; }
    }
    /// 
    /// A string to set the custom gradle tools version (ex: com.android.tools.build:gradle:4.2.0)
    /// 
    public string CustomGradleToolsVersion
    {
        get { return Instance.customGradleToolsVersion;  }
        set { Instance.customGradleToolsVersion = value; }
    }
    /// 
    /// AdMob Android App ID.
    /// 
    public string AdMobAndroidAppId
    {
        get { return Instance.adMobAndroidAppId; }
        set { Instance.adMobAndroidAppId = value; }
    }
    /// 
    /// AdMob iOS App ID.
    /// 
    public string AdMobIosAppId
    {
        get { return Instance.adMobIosAppId; }
        set { Instance.adMobIosAppId = value; }
    }
    /// 
    /// Saves the instance of the settings.
    /// 
    public void SaveAsync()
    {
        EditorUtility.SetDirty(_instance);
    }
}