//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
namespace CommonHelpers {

using System;

//**************************************************************************************************
partial class LnkProperties {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public string Arguments
{
   get { return (string)_Properties_[ LnkProperty.Arguments ]; }
   set { _Properties_[ LnkProperty.Arguments ] = NormalizeStringProperty( value ); }
}

public string Description
{
   get { return (string)_Properties_[ LnkProperty.Description ]; }
   set { _Properties_[ LnkProperty.Description ] = NormalizeStringProperty( value ); }
}

public string IconPath
{
   get { return (string)_Properties_[ LnkProperty.IconPath ]; }
   set { _Properties_[ LnkProperty.IconPath ] = NormalizeStringProperty( value ); }
}

public string Path
{
   get { return (string)_Properties_[ LnkProperty.Path ]; }
   set { _Properties_[ LnkProperty.Path ] = NormalizeStringProperty( value ); }
}

public string DisplayName
{
   get { return (string)_Properties_[ LnkProperty.DisplayName ]; }
   set { _Properties_[ LnkProperty.DisplayName ] = NormalizeStringProperty( value ); }
}

public string WorkingDirectory
{
   get { return (string)_Properties_[ LnkProperty.WorkingDirectory ]; }
   set { _Properties_[ LnkProperty.WorkingDirectory ] = NormalizeStringProperty( value ); }
}

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

public int? IconIndex
{
   get { return (int?)_Properties_[ LnkProperty.IconIndex ]; }
   set { _Properties_[ LnkProperty.IconIndex ] = HNullable.ToObject( value ); }
}

public int? ShowCmd
{
   get { return (int?)_Properties_[ LnkProperty.ShowCmd ]; }
   set { _Properties_[ LnkProperty.ShowCmd ] = HNullable.ToObject( value ); }
}

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

public ushort? HotKey
{
   get { return (ushort?)_Properties_[ LnkProperty.HotKey ]; }
   set { _Properties_[ LnkProperty.HotKey ] = HNullable.ToObject( value ); }
}

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

public bool RunAsAdministrator
{
   get { return (bool?)_Properties_[ LnkProperty.RunAsAdministrator ] ?? false; }
   set { _Properties_[ LnkProperty.RunAsAdministrator ] = value; }
}

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

public static bool IsSpecialProperty(LnkProperty value)
{
   return HArray.Contains( _SpecialProperties_, value );
}
public static bool IsSpecialProperty(string name)
{
   return HString.IsOneOf( true, name, _SpecialPropertyNames_ );
}

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

/// <exception cref="HOperationFailedException" />
public static LnkProperties LoadPropertiesFromTextFile(string filePath,HLnkConverter converter)
{
   var properties = new LnkProperties( converter,
      converter.Settings.HasTxtFlags ? new LnkVariableProcessor( converter.Settings ) : null );
   properties.LoadFromTextFile( filePath );
   return properties;
}

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

/// <exception cref="HOperationFailedException" />
public static LnkProperties LoadPropertiesFromShellLinkFile( string filePath,
                                                             HLnkConverter converter )
{
   var properties = new LnkProperties( converter );
   properties.LoadFromShellLinkFile( filePath );
   return properties;
}

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

LnkProperties(HLnkConverter converter,LnkVariableProcessor variableProcessor=null)
{
   _Converter_ = converter; _VariableProcessor_ = variableProcessor;
}

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

/// <exception cref="HOperationFailedException" />
HShellLinkProperties CreateShellLinkProperties()
{
   try {
      return new HShellLinkProperties(); }
   catch (PlatformNotSupportedException e) {
      throw HOpFailedExceptionCreator.Create( e ); }
}

string GetFilePathForErrorInfo(string filePath)
{
   return _Converter_.GetFilePathForErrorInfo( filePath );
}

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

static string NormalizeStringProperty(string value)
{
   return value!=null ?
      value.Replace(Environment.NewLine," ").Replace('\n',' ').Replace('\r',' ') :
      null;
}

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

const char LineCommentChar = '#';

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

readonly HSimpleDictionary<LnkProperty,object>
   _Properties_ = new HSimpleDictionary<LnkProperty,object>();
readonly HLnkConverter
   _Converter_;
readonly LnkVariableProcessor
   _VariableProcessor_;

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

static readonly LnkProperty[]
   _StringProperties_ = new LnkProperty[]
   {
      LnkProperty.Arguments,
      LnkProperty.Description,
      LnkProperty.IconPath,
      LnkProperty.Path,
      LnkProperty.DisplayName,
      LnkProperty.WorkingDirectory
   };
static readonly LnkProperty[]
   _IntegerProperties_ = new LnkProperty[]
   {
      LnkProperty.IconIndex,
      LnkProperty.ShowCmd
   };
static readonly LnkProperty[]
   _UShortProperties_ = new LnkProperty[]
   {
      LnkProperty.HotKey
   };
static readonly LnkProperty[]
   _FlagProperties_ = new LnkProperty[]
   {
      LnkProperty.RunAsAdministrator
   };
static readonly LnkProperty[]
   _ByteArrayProperties_ = new LnkProperty[]
   {
      LnkProperty.IDList
   };
static readonly LnkProperty[]
   _SpecialProperties_ = HArray.Concatenate( _FlagProperties_ );

static readonly string[]

   _StringPropertyNames_    = HArray.ToStrings( _StringProperties_    ),
   _IntegerPropertyNames_   = HArray.ToStrings( _IntegerProperties_   ),
   _UShortPropertyNames_    = HArray.ToStrings( _UShortProperties_    ),
   _FlagPropertyNames_      = HArray.ToStrings( _FlagProperties_      ),
   _ByteArrayPropertyNames_ = HArray.ToStrings( _ByteArrayProperties_ ),
   _SpecialPropertyNames_   = HArray.ToStrings( _SpecialProperties_   ),

   _PropertyNames_ = HArray.Concatenate
   (
      _StringPropertyNames_,
      _IntegerPropertyNames_,
      _UShortPropertyNames_,
      _FlagPropertyNames_,
      _ByteArrayPropertyNames_
   );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // LnkProperties
//**************************************************************************************************

} // CommonHelpers
