// Shawn Gano, ASIAirCleaner - removes _thn.jpg files from selected folder and all folders contained within
//  meant to run from the right-click menu for all windows folders (see Registry files to enable it)
//  requires .net v10
//
// v1.0 - 4 July 2026
//


using System;
using System.IO;


namespace ASIAirCleaner
{
    class ASIAirCleaner
    {
        // Global tracking variable for deleted files count
        private static int deletedFilesCount = 0;
        //private static int totalFilesFound = 0;

        static void Main(string[] args)
        {

            Console.WriteLine("******* ASIAirCleaner  v1.0.0  *******");


            // Validate that an argument was passed
            if (args.Length == 0)
            {
                Console.WriteLine("Error: Please provide a directory path as the main argument.");
                return;
            } // if no path provided

            // Correctly extract the path string from the args array index
            string targetDirectory = args[0];

            if (!Directory.Exists(targetDirectory))
            {
                Console.WriteLine($"Error: The directory '{targetDirectory}' does not exist.");
                return;
            } // if directory doesn't exist


            // Ask user if they want to delete all these files or not?  (TODO : List how many!)        
            DialogResult result = MessageBox.Show(
                "Are you sure you want to delete all _thn.jpg files from this folder and all sub folders?", 
                "ASIAirCleaner", 
                MessageBoxButtons.YesNo, 
                MessageBoxIcon.Question
            );

            // if user selects anything other than YES - terminate program
            if (result != DialogResult.Yes)
            {
                Console.WriteLine("User canceled ASIAirCleaner.");
                return;
            } // if not yes
            // ------------------


            Console.WriteLine($"Starting recursive deletion of *_thn.jpg files in: {targetDirectory}");
            Console.WriteLine("------------------");

            DeleteThumbnailFiles(targetDirectory);    //  PERFORM THE RECURSIVE DELETES! 

            Console.WriteLine("------------------");
            Console.WriteLine("Process completed.");
            Console.WriteLine("------------------");
            Console.WriteLine($"Total files deleted: {deletedFilesCount}");

            // Define the popup elements
            string message = $"Deletion of _thn.jpg files Completed.\n\nTotal files deleted: {deletedFilesCount}";
            string caption = "ASIAirCleaner";
    
            // Show the box with Yes and No buttons, and a Question icon
            DialogResult result2 = MessageBox.Show(
                message, 
                caption, 
                MessageBoxButtons.OK, 
                MessageBoxIcon.Information
            ); // results dialog


        } // Main

        private static void DeleteThumbnailFiles(string rootPath)
        {
            try
            {
                string[] files = Directory.GetFiles(rootPath, "*_thn.jpg", SearchOption.TopDirectoryOnly);

                foreach (string file in files)
                {
                    try
                    {
                        File.SetAttributes(file, FileAttributes.Normal);
                        File.Delete(file);
                        Console.WriteLine($"Deleted: {file}");

                        // Increment the global counter
                        deletedFilesCount++;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Could not delete {file}: {ex.Message}");
                    }
                }

                string[] subDirs = Directory.GetDirectories(rootPath);
                foreach (string dir in subDirs)
                {
                    DeleteThumbnailFiles(dir);
                }
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine($"Access denied to directory: {rootPath}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error scanning {rootPath}: {ex.Message}");
            }

        } // DeleteThumbnailFiles


    } //class ASIAirCleaner
    
} // namespace ASIAIRCleaner
