using System;
using System.Text;
using System.Collections.Generic;

using CommonHelpers;

namespace SqlCatPP {

//**************************************************************************************************
static partial class Program {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public const int
   ParamIdx_SettingsScriptFile    = 0,
   ParamIdx_ScriptSourcePattern   = 1,
   ParamIdx_InitialScriptFile     = 2,
   ParamIdx_SubfolderSourcePattern= 3,
   ParamIdx_OuputScriptFile       = 4;

public const string
   OrderListFileName = "$sql_order.list";

//--------------------------------------------------------------------------------------------------

public static string SettingsScriptPath      => _SettingsScriptPath_;
public static string ScriptSourcePattern     => _ScriptSourcePattern_;
public static string InitialScriptPath       => _InitialScriptPath_;
public static string SubfolderSourcePattern  => _SubfolderSourcePattern_;
/// <exception cref="InvalidOperationException" />
[HNotNull]
public static string OuputScriptPath
{
   get
   {
      if ( _OuputScriptPath_ == null )
         throw new InvalidOperationException();
      return _OuputScriptPath_;
   }
}

public static bool InsertSetVarComments      => _InsertSetVarComments_;
public static bool UnfoldEnvironmentVars     => _UnfoldEnvironmentVars_;
public static bool ProcessDirectives         => _ProcessDirectives_;
public static bool OrderListErrorAsWarn      => _OrderListErrorAsWarn_;
public static bool AllowEmptyResult          => _AllowEmptyResult_;
public static bool DefaultInputEncodingUTF8  => _DefaultInputEncodingUTF8_;
public static bool CheckInputForANSI         => _CheckInputForANSI_;
public static bool OutputAsANSI              => _OutputAsANSI_;
public static string OutputFolderPath        => _OutputFolderPath_;
public static string OutputFileName          => _OutputFileName_;

public static GeneratedParams GeneratedParams => _GeneratedParams_;

public static bool UsePP => ProcessDirectives || InsertSetVarComments;
public static bool IsMultipleTranslation =>
   _IsMultipleTranslation_ ??= 
      ScriptSourcePattern != null && IsFilePattern( ScriptSourcePattern ) ||
      SubfolderSourcePattern != null;

public static Encoding InputEncoding =>
   DefaultInputEncodingUTF8 ? Encoding.UTF8 : Encoding.Default;

static bool IsFilePattern(string filePath)
{
   if ( filePath.IndexOf('*') >= 0 )
      return true;
   int idx = filePath.IndexOf('?');
   if ( idx < 0 )
      return false;
   const string slashes = "\\/";
   idx = 0;
   if ( filePath.Length >= 4 &&
        slashes.IndexOf( filePath[0] ) >= 0 &&
        slashes.IndexOf( filePath[1] ) >= 0 &&
        filePath[2] == '?' &&
        slashes.IndexOf( filePath[3] ) >= 0 )
      idx = 4;
   return filePath.IndexOf( '?', idx ) >= 0;
}

//==================================================================================================

public static int WarningCount => _WarningList_.Count;
public static int TotallyScriptFiles => _TotallyScriptFiles_;

//==================================================================================================

public static void IncrementTotallyScriptFiles() =>
   _TotallyScriptFiles_++;

public static void AddWarning(string text) =>
   _WarningList_.Add( text );

//==================================================================================================

/// <exception cref="HAppFailureException" />
static void PerformConcatenation()
{
   try
   {
      _Scripter_ = new Scripter();

      if ( SettingsScriptPath != null )
         ProcessSingleSource( SettingsScriptPath );
      if ( InitialScriptPath != null )
         ProcessSingleSource( InitialScriptPath );
      if ( SubfolderSourcePattern != null )
         ProcessSourceFolders( SubfolderSourcePattern );
      if ( ScriptSourcePattern != null )
         ProcessSourceFiles( ScriptSourcePattern );

      _Scripter_.CompleteOutput( OuputScriptPath );
   }
   catch (Exception e)
   {
      if (e is OperationFailedException)
         throw HAppFailureExceptionCreator.Create( e );
      throw;
   }
   finally
   {
      _Scripter_ = null;
   }
}

//==================================================================================================

static readonly GeneratedParams
   _GeneratedParams_ = new ();
static readonly List<string>
   _WarningList_ = new ();

static string
   _SettingsScriptPath_, _ScriptSourcePattern_,
   _InitialScriptPath_, _SubfolderSourcePattern_,
   _OuputScriptPath_, _OutputFolderPath_, _OutputFileName_;
static bool
   _InsertSetVarComments_     = AdditionalParams.DefaultValues.InsertSetVarComments,
   _UnfoldEnvironmentVars_    = AdditionalParams.DefaultValues.UnfoldEnvironmentVars,
   _ProcessDirectives_        = AdditionalParams.DefaultValues.ProcessDirectives,
   _OrderListErrorAsWarn_     = AdditionalParams.DefaultValues.OrderListErrorAsWarn,
   _AllowEmptyResult_         = AdditionalParams.DefaultValues.AllowEmptyResult,
   _DefaultInputEncodingUTF8_ = AdditionalParams.DefaultValues.DefaultInputEncodingUTF8,
   _CheckInputForANSI_        = AdditionalParams.DefaultValues.CheckInputForANSI,
   _OutputAsANSI_             = AdditionalParams.DefaultValues.OutputAsANSI;
static bool?
   _IsMultipleTranslation_;
static Scripter
   _Scripter_;
static int
   _TotallyScriptFiles_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Program
//**************************************************************************************************

} // SqlCatPP
