// Copyright © Selmo Technology GmbH. All Rights Reserved

using Newtonsoft.Json;
using SelmoStudio.Hmi.Common.SelmoProject.ProcessValueDefinition;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Model;

namespace SelmoStudio.Hmi.Server.ProjectData
{
    public static class ProjectDataHelper
    {
        public const string ProjectDataFileName = "selmoproject.json";
        public const string ProjectValueDefinitionFileName = "processValueDefinitions.json";
        public const string ProjectDataFolderName = "ProjectData";
        public const string PatchFolderName = "Patch";

        public static ProcessValuesPlantDefinition GetProcessValueDefinitions()
        {
            var path = GetRelativePath(ProjectValueDefinitionFileName);
            string dataAsString = File.ReadAllText(path);
            var processValueDefinitions = JsonConvert.DeserializeObject<ProcessValuesPlantDefinition>(dataAsString)!;
            return processValueDefinitions;
        }

        public static SelmoHmiProject GetProject()
        {
            var path = GetRelativePath(ProjectDataFileName);
            string dataAsString = File.ReadAllText(path);
            var processValueDefinitions = JsonConvert.DeserializeObject<SelmoHmiProject>(dataAsString)!;
            return processValueDefinitions;
        }

        private static string GetRelativePath(string fileName)
        {
            var appBasePath = AppDomain.CurrentDomain.BaseDirectory;

#if DEBUG
            var fullFilePathPatch = Path.Combine(appBasePath, ProjectDataFolderName, PatchFolderName, fileName);
            if (File.Exists(fullFilePathPatch))
            {
                return fullFilePathPatch;
            }
#endif

            var fullFilePath = Path.Combine(appBasePath, ProjectDataFolderName, fileName);
            return fullFilePath;
        }
    }
}