﻿using System.IO;
using Platform101XP.Editor.Settings.Repositories;
using UnityEngine;

namespace Platform101XP.Editor.Settings.Export
{
    public abstract class BaseSettingsExporter
    {
        protected readonly BaseSettingsRepository BaseSettings;

        public abstract void Export();

        protected BaseSettingsExporter(BaseSettingsRepository baseSettings)
        {
            this.BaseSettings = baseSettings;
        }

        protected void WriteParameterIntoFile(string targetPath)
        {
            if (!File.Exists(targetPath))
            {
                Debug.LogError($"[P101XP] The settings file has not found!\nPath: {targetPath}");
                return;
            }

            var text = File.ReadAllText(targetPath);
            if (string.IsNullOrEmpty(text))
            {
                Debug.LogError($"[P101XP] The settings file has been damaged!\nPath: {targetPath}");
                return;
            }

            // Write parameters into file by replacing special placeholders.
            text = ReplaceParametersInText(text);

            File.WriteAllText(targetPath, text);
            Debug.Log($"[P101XP] The settings have been inserted into settings file.\nPath: {targetPath}");
        }

        // It is possible to override this method to implement replacing of custom parameters.
        // NOTE: Do not forget to call base implementation if you are overriding.
        protected virtual string ReplaceParametersInText(string text)
        {
            text = text.Replace("{client_id}", BaseSettings.ClientId);
            text = text.Replace("{app_name}", BaseSettings.AppName);
            text = text.Replace("{app_config_name}", BaseSettings.AppConfigName);
            text = text.Replace("{dev_mode}", BaseSettings.DevMode.ToString().ToLower());
            text = text.Replace("{web_store_project_name}", BaseSettings.WebStoreProjectName.ToString().ToLower());
            text = text.Replace("{auto_login_enabled}", BaseSettings.AutoLoginEnabled.ToString().ToLower());
            text = text.Replace("{guest_timeout_enabled}", BaseSettings.GuestTimeoutEnabled.ToString().ToLower());
            text = text.Replace("{guest_only_auth}", BaseSettings.GuestOnlyAuth.ToString().ToLower());
            text = text.Replace("{web_support_url}", BaseSettings.WebSupportUrl);
            text = text.Replace("{zendesk_game_id}", BaseSettings.ZendeskGameId);
            text = text.Replace("{zendesk_category_id}", BaseSettings.ZendeskCategoryId);
            text = text.Replace("{zendesk_app_id}", BaseSettings.ZendeskAppId);
            text = text.Replace("{zendesk_auth_client_id}", BaseSettings.ZendeskAuthClientId);
            text = text.Replace("{billing_test}", BaseSettings.BillingTest.ToString().ToLower());
            text = text.Replace("{appsflyer_devkey}", BaseSettings.AppsflyerDevKey);
            text = text.Replace("{appsflyer_track_enabled}", BaseSettings.AppsflyerTrackEnabled.ToString().ToLower());
            text = text.Replace("{appsflyer_debug_log_enabled}", BaseSettings.AppsflyerDebugLogEnabled.ToString().ToLower());
            text = text.Replace("{app_review_days_delay}", BaseSettings.AppReviewDaysSelay.ToString());
            text = text.Replace("{app_review_seconds_delay}", BaseSettings.AppReviewSecondsDelay.ToString());
            text = text.Replace("{app_review_enabled}", BaseSettings.AppReviewEnabled.ToString().ToLower());
            text = text.Replace("{app_store_link}", BaseSettings.AppStoreLink);
            text = text.Replace("{app_store_id}", BaseSettings.AppStoreId);
            text = text.Replace("{eula_show_enabled}", BaseSettings.EulaShowEnabled.ToString().ToLower());
            text = text.Replace("{eula_auto_accept}", BaseSettings.EulaAutoAccept.ToString().ToLower());
            text = text.Replace("{eula_default_url}", BaseSettings.EulaDefaultUrl);
            text = text.Replace("{device_token_enabled}", BaseSettings.DeviceTokenEnabled.ToString().ToLower());
            text = text.Replace("{web_ui_enabled}", BaseSettings.WebUiEnabled.ToString().ToLower());
            text = text.Replace("{web_ui_version}", BaseSettings.WebUiVersion);
            return text;
        }
    }
}
