﻿using System;
using System.Collections.Generic;

namespace SqlToolbox.Application
{
    internal struct DatabaseScript
    {
        #region Fields

        internal readonly string DatabaseName;
        internal readonly string ScriptPath;

        #endregion

        #region Constructors

        internal DatabaseScript(string name, string path)
            : this()
        {
            DatabaseName = name;
            ScriptPath = path;
        }

        #endregion

        #region Static methods

        public static DatabaseScript GetDatabaseScript(IEnumerable<string> variables)
        {
            var info = new DatabaseScript();

            foreach (var var in variables)
            {
                var separatorIndex = var.IndexOf('=');
                var name = var.Substring(0, separatorIndex).Trim().ToLower();
                var value = var.Substring(separatorIndex + 1).Trim();

                switch (name)
                {
                    case "name":
                        info = new DatabaseScript(value, info.ScriptPath);
                        break;
                    case "scripts":
                        info = new DatabaseScript(info.DatabaseName, value);
                        break;
                    default:
                        throw new ArgumentException();
                }
            }

            return info;
        }

        #endregion
    }
}
