//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.IO;
using System.ComponentModel;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
[Flags]
public enum HFileDirDeletionOptions
{
   None = 0,
   RemoveReadonly      = 1 << 0,
   IgnoreNotFoundError = 1 << 1
}
//**************************************************************************************************


//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HFile {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="ArgumentNullException" />
public static bool CheckExists(string path,out Exception error)
{
   return CheckExists( path, false, out error );
}

/// <exception cref="ArgumentNullException" />
public static bool CheckNotExists(string path,out Exception error)
{
   return CheckNotExists( path, false, out error );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="HOperationFailedException" />
public static bool CheckIfExistsOrThrow(string path)
{
   return CheckIfExistsOrThrow( path, false );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
public static void VerifyExists(string path)
{
   VerifyExists( path, false );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
internal static void VerifyNotExists(string path)
{
   VerifyNotExists( path, false );
}

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

/// <exception cref="ArgumentNullException" />
public static bool CheckFileNameExists(string path,out Exception error)
{
   return CheckExists( path, null, out error );
}

/// <exception cref="ArgumentNullException" />
public static bool CheckFileNameNotExists(string path,out Exception error)
{
   return CheckNotExists( path, null, out error );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
public static void VerifyFileNameExists(string path)
{
   VerifyExists( path, null );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
internal static void VerifyFileNameNotExists(string path)
{
   VerifyNotExists( path, null );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
public static bool ExamineIsReadonly(string path)
{
   return ExamineIsReadonly( path, false );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static void SetReadonlyAttribute(string path)
{
   SetReadonlyAttribute( path, false );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static void ResetReadonlyAttribute(string path)
{
   ResetReadonlyAttribute( path, false );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
public static FileAttributes GetAttributes(string path)
{
   return GetAttributes( path, false );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static void SetAttributes(string path,FileAttributes attributes)
{
   SetAttributes( path, false, attributes );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static FileAttributes AddAttributes(string path,FileAttributes attributes)
{
   return AddAttributes( path, false, attributes );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static FileAttributes RemoveAttributes(string path,FileAttributes attributes)
{
   return RemoveAttributes( path, false, attributes );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static FileAttributes ModifyAttributes( string path,
                                               FileAttributes attributesToAdd,
                                               FileAttributes attributesToRemove )
{
   return ModifyAttributes( path, false, attributesToAdd, attributesToRemove );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
public static void Delete(string path)
{
   Delete( path, false );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="HIncorrectFilePathException" />
public static void Delete(string path,bool ignoreNotFoundError)
{
   Delete( path,
      ( ignoreNotFoundError ?
      HFileDirDeletionOptions.IgnoreNotFoundError : 0 ) );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
public static void Delete(string path,HFileDirDeletionOptions options)
{
   VerifyPath( path, false );
   try
   {
      if ( (options & HFileDirDeletionOptions.RemoveReadonly) != 0 )
         RemoveAttributes( path, FileAttributes.ReadOnly );
      File.Delete( path );
   }
   catch (FileNotFoundException)
   {
      if ( (options & HFileDirDeletionOptions.IgnoreNotFoundError) == 0 )
         throw CreateFileNotFoundException( path, false );
   }
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static void DeleteReadonly(string path)
{
   DeleteReadonly( path, false );
}

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static void DeleteReadonly(string path,bool ignoreNotFoundError)
{
   Delete( path,
      HFileDirDeletionOptions.RemoveReadonly |
      ( ignoreNotFoundError ?
        HFileDirDeletionOptions.IgnoreNotFoundError : 0 ) );
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
/// <exception cref="FileNotFoundException" />
/// <exception cref="HIncorrectFilePathException" />
/// <exception cref="HOperationFailedException" />
public static void Move(string sourcePath,string tragetPath)
{
   Move( sourcePath, tragetPath, false );
}

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

static HFile()
{
   CommonHelpers_Core.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HFile
//**************************************************************************************************

} // CommonHelpers (.WASM)
