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

using System;

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

/// <exception cref="HOperationFailedException" />
public void LoadFromShellLinkFile(string filePath)
{
   var shlp = CreateShellLinkProperties();
   try
   {
      HShellLinkSpecialProps specialProps;

      shlp.Load( filePath, out specialProps );

      Arguments        = HString.EmptyAsNull( shlp.Arguments );
      Description      = HString.EmptyAsNull( shlp.Description );
      Path             = HString.EmptyAsNull( shlp.Path );
      WorkingDirectory = HString.EmptyAsNull( shlp.WorkingDirectory );

      if ( Path == null )
         DisplayName = HString.EmptyAsNull( shlp.DisplayName );

      int iconIndex;
      IconPath = HString.EmptyAsNull(
         shlp.GetIconLocation(out iconIndex) );
      IconIndex = iconIndex;

      HotKey = shlp.Hotkey;
      ShowCmd = shlp.ShowCmd;

      RunAsAdministrator = specialProps.RunAsAdministrator;

      CheckRequiredProperties( shlp, filePath );
   }
   catch
   {
      HDisposable.TryDispose( ref shlp );
      throw;
   }
   shlp.Dispose();
}

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

/// <exception cref="HOperationFailedException" />
public void SaveToShellLinkFile(string filePath)
{
   var shlp = CreateShellLinkProperties();
   try
   {
      if ( Path != null )
      {
         string dummyShortcutFilePath = shlp.IsProbablyExecutablePath( Path ) ?
            _Converter_.Settings.ExeDummyShortcutFilePath :
            _Converter_.Settings.FolderDummyShortcutFilePath;
         if ( dummyShortcutFilePath != null )
            try {
               shlp.Load( dummyShortcutFilePath ); }
            catch (HOperationFailedException e) {
               throw HOpFailedExceptionCreator.Create(
                  HLnkConverter.Verbose.CAN_NOT_LOAD_DUMMY_SHORTCUT_FILE_0__1,
                  GetFilePathForErrorInfo( dummyShortcutFilePath ), e.Message ); }
      }

      shlp.BeginEnvironmentObstruction();

      shlp.Path = HString.NullAsEmpty( Path );

      shlp.Arguments        = HString.NullAsEmpty( Arguments );
      shlp.Description      = HString.NullAsEmpty( Description );
      shlp.WorkingDirectory = HString.NullAsEmpty( WorkingDirectory );

      if ( Path == null )
         shlp.DisplayName = HString.NullAsEmpty( DisplayName );

      shlp.SetIconLocation( HString.NullAsEmpty( IconPath ), IconIndex ?? 0 );
      shlp.Hotkey = HotKey ?? 0;
      shlp.ShowCmd = ShowCmd ?? 0;

      CheckRequiredProperties( shlp, filePath, true );

      shlp.Save( filePath, new HShellLinkSpecialProps(){
         RunAsAdministrator = this.RunAsAdministrator } );
   }
   catch
   {
      HDisposable.TryDispose( ref shlp );
      throw;
   }
   shlp.Dispose();
}

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

/// <exception cref="HOperationFailedException" />
void CheckRequiredProperties(HShellLinkProperties shlp,string filePath,bool savingAttempt=false)
{
   if ( string.IsNullOrWhiteSpace( shlp.Path ) &&
        string.IsNullOrWhiteSpace( shlp.DisplayName ) )
      throw HOpFailedExceptionCreator.Create(
         HLnkConverter.Verbose.
         PRESENCE_OF_AT_LEAST_ONE_OF_SHORTCUT_PROPERTIES_IS_REQUIRED_PATH_DISPLAYNAME__0_1,
         savingAttempt ?
            HLnkConverter.Verbose.AttemptToSaveInFile :
            HLnkConverter.Verbose.File,
         GetFilePathForErrorInfo( filePath ) );
}

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

} // CommonHelpers
