}         //end write all jpegs

        /* Write JSON to .stack File  */
        public bool writeJSONFile(int m1, int m5, int m25, int m100, int m250, String tag)
        {
            bool jsonExported = true;
            int  totalSaved   = m1 + (m5 * 5) + (m25 * 25) + (m100 * 100) + (m250 * 250);
            // Track the total coins
            int coinCount = m1 + m5 + m25 + m100 + m250;

            String[] coinsToDelete    = new String[coinCount];
            String[] bankedFileNames  = new DirectoryInfo(this.fileSystem.BankFolder).GetFiles().Select(o => o.Name).ToArray();//Get all names in bank folder
            String[] frackedFileNames = new DirectoryInfo(this.fileSystem.FrackedFolder).GetFiles().Select(o => o.Name).ToArray();;
            String[] partialFileNames = new DirectoryInfo(this.fileSystem.PartialFolder).GetFiles().Select(o => o.Name).ToArray();
            // Add the two arrays together
            var list = new List <String>();

            list.AddRange(bankedFileNames);
            list.AddRange(frackedFileNames);
            list.AddRange(partialFileNames);

            // Program will spend fracked files like perfect files
            bankedFileNames = list.ToArray();


            // Check to see the denomination by looking at the file start
            int c = 0;
            // c= counter
            String json = "{" + Environment.NewLine;

            json = json + "\t\"cloudcoin\": " + Environment.NewLine;
            json = json + "\t[" + Environment.NewLine;
            String bankFileName;
            String frackedFileName;
            String partialFileName;
            string denomination;
            Stack  stack = new Stack();

            // Put all the JSON together and add header and footer
            for (int i = 0; (i < bankedFileNames.Length); i++)
            {
                denomination    = bankedFileNames[i].Split('.')[0];
                bankFileName    = this.fileSystem.BankFolder + bankedFileNames[i];    //File name in bank folder
                frackedFileName = this.fileSystem.FrackedFolder + bankedFileNames[i]; //File name in fracked folder
                partialFileName = this.fileSystem.PartialFolder + bankedFileNames[i];
                if (denomination == "1" && m1 > 0)
                {
                    if (c != 0)//This is the json seperator between each coin. It is not needed on the first coin
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName)) // Is it a bank file
                    {
                        CloudCoin coinNote = fileSystem.LoadCoin(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileSystem.LoadCoin(partialFileName);
                        //coinNote = fileSystem.loa
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m1--;
                    // Get the clean JSON of the coin
                }// end if coin is a 1

                if (denomination == "5" && m5 > 0)
                {
                    if ((c != 0))
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName))
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileSystem.LoadCoin(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m5--;
                } // end if coin is a 5

                if (denomination == "25" && m25 > 0)
                {
                    if ((c != 0))
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName))
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileSystem.LoadCoin(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m25--;
                }// end if coin is a 25

                if (denomination == "100" && m100 > 0)
                {
                    if ((c != 0))
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName))
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileSystem.LoadCoin(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m100--;
                } // end if coin is a 100

                if (denomination == "250" && m250 > 0)
                {
                    if ((c != 0))
                    {
                        json += ",\n";
                    }

                    if (File.Exists(bankFileName))
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(bankFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = bankFileName;
                        c++;
                    }
                    else if (File.Exists(partialFileName)) // Is it a partial file
                    {
                        CloudCoin coinNote = fileSystem.LoadCoin(partialFileName);
                        coinNote.aoid    = null;//Clear all owner data
                        json             = json + fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = partialFileName;
                        c++;
                    }
                    else
                    {
                        CloudCoin coinNote = this.fileSystem.LoadCoin(frackedFileName);
                        coinNote.aoid    = null;
                        json             = json + this.fileSystem.setJSON(coinNote);
                        coinsToDelete[c] = frackedFileName;
                        c++;
                    }

                    m250--;
                }// end if coin is a 250

                if (m1 == 0 && m5 == 0 && m25 == 0 && m100 == 0 && m250 == 0)
                {
                    break;
                } // Break if all the coins have been called for.
            }     // end for each coin needed

            /*WRITE JSON TO FILE*/
            json  = json + "\t] " + Environment.NewLine;
            json += "}";
            String filename = (this.fileSystem.ExportFolder + Path.DirectorySeparatorChar + totalSaved + ".CloudCoins." + tag + ".stack");

            if (File.Exists(filename))
            {
                // tack on a random number if a file already exists with the same tag
                Random rnd     = new Random();
                int    tagrand = rnd.Next(999);
                filename = (this.fileSystem.ExportFolder + Path.DirectorySeparatorChar + totalSaved + ".CloudCoins." + tag + tagrand + ".stack");
            }//end if file exists

            File.WriteAllText(filename, json);
            Console.Out.WriteLine("Writing to : ");
            //CoreLogger.Log("Writing to : " + filename);
            Console.Out.WriteLine(filename);
            /*DELETE FILES THAT HAVE BEEN EXPORTED*/
            for (int cc = 0; cc < coinsToDelete.Length; cc++)
            {
                // Console.Out.WriteLine("Deleting " + coinsToDelete[cc]);
                if (coinsToDelete[cc] != null)
                {
                    File.Delete(coinsToDelete[cc]);
                }
            }//end for all coins to delete

            // end if write was good
            return(jsonExported);
        }//end write json to file
        }//end fix one



        /* PUBLIC METHODS */
        public int[] FixAll()
        {
            IsFixing = true;
            continueExecution = true;
            int[] results = new int[3];
            String[] frackedFileNames = new DirectoryInfo(this.fileUtils.FrackedFolder).GetFiles().Select(o => o.Name).ToArray();


            CloudCoin frackedCC;

            ProgressChangedEventArgs pge = new ProgressChangedEventArgs();
            pge.MajorProgressMessage = "Starting Frack Fixing";
            raida.OnLogRecieved(pge);

            //CoinUtils cu = new CoinUtils(frackedCC);
            if (frackedFileNames.Length < 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Out.WriteLine("You have no fracked coins.");
                //CoreLogger.Log("You have no fracked coins.");
                Console.ForegroundColor = ConsoleColor.White;
            }//no coins to unfrack



            for (int i = 0; i < frackedFileNames.Length; i++)
            {
                if (!continueExecution)
                {
                    Debug.WriteLine("Aborting Fix 1");
                    break;
                }
                Console.WriteLine("Unfracking coin " + (i + 1) + " of " + frackedFileNames.Length);
                //ProgressChangedEventArgs pge = new ProgressChangedEventArgs();
                pge.MajorProgressMessage = "Unfracking coin " + (i + 1) + " of " + frackedFileNames.Length;
                raida.OnLogRecieved(pge);
                //CoreLogger.Log("UnFracking coin " + (i + 1) + " of " + frackedFileNames.Length);
                try
                {
                    frackedCC = fileUtils.LoadCoin(this.fileUtils.FrackedFolder + frackedFileNames[i]);
                    if (frackedCC == null)
                        throw new IOException();
                    CoinUtils cu = new CoinUtils(frackedCC);
                    String value = frackedCC.pown;
                    //  Console.WriteLine("Fracked Coin: ");
                    cu.consoleReport();

                    CoinUtils fixedCC = fixCoin(frackedCC); // Will attempt to unfrack the coin. 
                    if (!continueExecution)
                    {
                        Debug.WriteLine("Aborting Fix 2");
                        break;
                    }
                    cu.consoleReport();
                    switch (fixedCC.getFolder().ToLower())
                    {
                        case "bank":
                            this.totalValueToBank++;
                            this.fileUtils.overWrite(this.fileUtils.BankFolder, fixedCC.cc);
                            this.deleteCoin(this.fileUtils.FrackedFolder + frackedFileNames[i]);
                            Console.WriteLine("CloudCoin was moved to Bank.");
                            pge.MajorProgressMessage = "CloudCoin was moved to Bank.";
                            raida.OnLogRecieved(pge);

                            //CoreLogger.Log("CloudCoin was moved to Bank.");
                            break;
                        case "counterfeit":
                            this.totalValueToCounterfeit++;
                            this.fileUtils.overWrite(this.fileUtils.CounterfeitFolder, fixedCC.cc);
                            this.deleteCoin(this.fileUtils.FrackedFolder + frackedFileNames[i]);
                            Console.WriteLine("CloudCoin was moved to Trash.");
                            pge.MajorProgressMessage = "CloudCoin was moved to Trash.";
                            raida.OnLogRecieved(pge);

                            //CoreLogger.Log("CloudCoin was moved to Trash.");
                            break;
                        default://Move back to fracked folder
                            this.totalValueToFractured++;
                            this.deleteCoin(this.fileUtils.FrackedFolder + frackedFileNames[i]);
                            this.fileUtils.overWrite(this.fileUtils.FrackedFolder, fixedCC.cc);
                            Console.WriteLine("CloudCoin was moved back to Fracked folder.");
                            pge.MajorProgressMessage = "CloudCoin was moved back to Fracked folder.";
                            raida.OnLogRecieved(pge);

                            //CoreLogger.Log("CloudCoin was moved back to Fraked folder.");
                            break;
                    }
                    // end switch on the place the coin will go 
                    Console.WriteLine("...................................");
                    pge.MajorProgressMessage = "...................................";
                    raida.OnLogRecieved(pge);

                    Console.WriteLine("");
                }
                catch (FileNotFoundException ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ex);
                    //CoreLogger.Log(ex.ToString());
                    Console.ForegroundColor = ConsoleColor.White;
                }
                catch (IOException ioex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ioex);
                    //CoreLogger.Log(ioex.ToString());
                    Console.ForegroundColor = ConsoleColor.White;
                } // end try catch
            }// end for each file name that is fracked

            results[0] = this.totalValueToBank;
            results[1] = this.totalValueToCounterfeit; // System.out.println("Counterfeit and Moved to trash: "+totalValueToCounterfeit);
            results[2] = this.totalValueToFractured; // System.out.println("Fracked and Moved to Fracked: "+ totalValueToFractured);
            IsFixing = false;
            continueExecution = true;
            pge.MajorProgressMessage = "Finished Frack Fixing.";
            raida.OnLogRecieved(pge);

            pge.MajorProgressMessage = "Fixed " + totalValueToBank + " CloudCoins and moved them into Bank Folder";
            if (totalValueToBank > 0)
                raida.OnLogRecieved(pge);

            return results;
        }// end fix all