//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
#if LEGACY
using System.Security.Permissions;
#endif // LEGACY

namespace CommonHelpers {

//**************************************************************************************************
/// <summary>
/// </summary>
#if LEGACY
[PermissionSet(SecurityAction.Demand,Unrestricted=true)]
#endif // LEGACY
[HFullFunctionalModeRequired]
public partial class HLnkConverter {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="ArgumentNullException" />
public HLnkConverterSettings Settings
{
   get { return _Settings_; }
   set { HArgChecking.VerifyNotNull( value ); _Settings_ = value; }
}

public bool IsUnderSupportedEnvironment
{
   get { return HOperatingSystem.IsWindowsVistaOrLater; }
}

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

/// <exception cref="PlatformNotSupportedException" />
public HLnkConverter(): this( null )
{}
/// <exception cref="PlatformNotSupportedException" />
public HLnkConverter([HOptional] HLnkConverterSettings settings)
{
   HOperatingSystem.VerifyWindowsVistaOrLater();
   _Settings_ = settings ?? new HLnkConverterSettings();
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="HOperationFailedException" />
public void ConvertLnkToTxt(string sourceFilePath,string targetFilePath)
{
   HArgChecking.VerifyNotNulls( sourceFilePath, targetFilePath );
   LnkProperties lnkProperties;
   try
   {
      lnkProperties = LnkProperties.LoadPropertiesFromShellLinkFile( sourceFilePath, this );
   }
   catch (Exception e)
   {
      if ( e is HOperationFailedException )
         throw CreateCantLoadPropertiesError( sourceFilePath, e );
      throw CreateUnexpectedErrorDuringFileLoad( sourceFilePath, e );
   }
   try
   {
      lnkProperties.SaveToTextFile( targetFilePath );
   }
   catch (Exception e)
   {
      if ( e is HOperationFailedException )
         throw CreateCantSavePropertiesError( targetFilePath, e );
      throw CreateUnexpectedErrorDuringFileCreation( targetFilePath, e );
   }
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="HOperationFailedException" />
public void ConvertTxtToLnk(string sourceFilePath,string targetFilePath)
{
   HArgChecking.VerifyNotNulls( sourceFilePath, targetFilePath );
   LnkProperties lnkProperties;
   try
   {
      lnkProperties = LnkProperties.LoadPropertiesFromTextFile( sourceFilePath, this );
   }
   catch (Exception e)
   {
      if ( e is HOperationFailedException )
         throw CreateCantLoadPropertiesError( sourceFilePath, e );
      throw CreateUnexpectedErrorDuringFileLoad( sourceFilePath, e );
   }
   try
   {
      lnkProperties.SaveToShellLinkFile( targetFilePath );
   }
   catch (Exception e)
   {
      if ( e is HOperationFailedException )
         throw CreateCantSavePropertiesError( targetFilePath, e );
      throw CreateUnexpectedErrorDuringFileCreation( targetFilePath, e );
   }
}

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

HOperationFailedException CreateCantLoadPropertiesError(string sourceFilePath,Exception e)
{
   return HOpFailedExceptionCreator.Create(
      HLnkConverter.Verbose.CAN_NOT_LOAD_SHORTCUT_PROPERTIES_FROM_FILE_0__1,
      GetFilePathForErrorInfo( sourceFilePath ), e.Message );
}
HOperationFailedException CreateCantSavePropertiesError(string targetFilePath,Exception e)
{
   return HOpFailedExceptionCreator.Create(
      HLnkConverter.Verbose.CAN_NOT_SAVE_SHORTCUT_PROPERTIES_TO_FILE_0__1,
      GetFilePathForErrorInfo( targetFilePath ), e.Message );
}

HOperationFailedException CreateUnexpectedErrorDuringFileLoad(string sourceFilePath,Exception e)
{
   return HOpFailedExceptionCreator.Create(
      HLnkConverter.Verbose.
      UNEXPECTED_ERROR_DURING_LOADING_OF_SHORTCUT_PROPERTIES_FROM_FILE_0__1__2,
      GetFilePathForErrorInfo( sourceFilePath ),
      HLnkConverter.Verbose.ErrorInformation,
      HException.ComposeChainedMessage( e, true ) );
}
HOperationFailedException CreateUnexpectedErrorDuringFileCreation( string targetFilePath,
                                                                    Exception e )
{
   return HOpFailedExceptionCreator.Create(
      HLnkConverter.Verbose.
      UNEXPECTED_ERROR_DURING_SAVING_OF_SHORTCUT_PROPERTIES_TO_FILE_0__1__2,
      GetFilePathForErrorInfo( targetFilePath ),
      HLnkConverter.Verbose.ErrorInformation,
      HException.ComposeChainedMessage( e, true ) );
}

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

internal string GetFilePathForErrorInfo(string filePath)
{
   return HPath.WithoutLongNamePrefix( Settings.PreferFullFilePathsForErrorInfo ?
      HPath.GetFullPathIfPossible( filePath ) : filePath );
}

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

static HLnkConverter()
{
   CommonHelpers_SysUtils.Access();
}

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

HLnkConverterSettings
   _Settings_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HLnkConverter
//**************************************************************************************************

} // CommonHelpers
