using System;
using System.IO;
using MarketData.CNNProcessing;
using MarketData.Utils;

namespace CNNImageProcessor
{
  class Program
  {

// **************************************************************************************************************************************************
// **************************************************************************************************************************************************

    // You will then need to copy this data to C:\DeepLearningImageTests\DeepLearningImageData\Data
    public static void GenerateImageData()
    {
      GenerateImageData(@"C:\DeepLearningImageTests\DeepLearningImageData\RawData0",@"c:\DeepLearningImageTests\DeepLearningImageData\Data\0");
      GenerateImageData(@"C:\DeepLearningImageTests\DeepLearningImageData\RawData1",@"c:\DeepLearningImageTests\DeepLearningImageData\Data\1");
    }

    public static void GenerateImageData(String inputFolder,String destinationFolder)
    {
      ImageHelper imageHelper = new ImageHelper();
      String[] files = Directory.GetFiles(inputFolder, "*.jpg");
      foreach (String file in files)
      {
        try
        {
          String fileName = Path.GetFileName(file);
          String pathFileName = destinationFolder + @"\" + fileName;
          String pathFileNameL = destinationFolder + @"\" + Utility.BetweenString(fileName,null,".")+"L.jpg";
          String pathFileNameR = destinationFolder + @"\" + Utility.BetweenString(fileName,null,".")+"R.jpg";
          String pathFileNameU = destinationFolder + @"\" + Utility.BetweenString(fileName,null,".")+"U.jpg";
          String pathFileNameB1 = destinationFolder + @"\" + Utility.BetweenString(fileName,null,".")+"B1.jpg";
          String pathFileNameB2 = destinationFolder + @"\" + Utility.BetweenString(fileName,null,".")+"B2.jpg";
          String pathFileNameB3 = destinationFolder + @"\" + Utility.BetweenString(fileName,null,".")+"B3.jpg";
          String pathFileNameB4 = destinationFolder + @"\" + Utility.BetweenString(fileName,null,".")+"B4.jpg";

          Console.WriteLine(String.Format("Reading {0}", file));
          imageHelper.LoadImage(file);
          imageHelper.Resize(128, 128);

          ImageHelper bmpLeft=new ImageHelper(imageHelper);
          ImageHelper bmpRight=new ImageHelper(imageHelper);
          ImageHelper bmpUDown=null;
          bmpLeft.RotateLeft();
          bmpRight.RotateRight();
          bmpUDown=new ImageHelper(bmpRight);
          bmpUDown.RotateRight();

          //bmpLeft.ToGrayScale();
          //bmpRight.ToGrayScale();
          //bmpUDown.ToGrayScale();
          //imageHelper.ToGrayScale();

          imageHelper.Save(pathFileName);
          bmpLeft.Save(pathFileNameL);
          bmpRight.Save(pathFileNameR);
          bmpUDown.Save(pathFileNameU);

          ImageHelper bmpBlur1=new ImageHelper(imageHelper);
          ImageHelper bmpBlur2=new ImageHelper(bmpLeft);
          ImageHelper bmpBlur3=new ImageHelper(bmpRight);
          ImageHelper bmpBlur4=new ImageHelper(bmpUDown);
          bmpBlur1.Blur(1);
          bmpBlur2.Blur(1);
          bmpBlur3.Blur(1);
          bmpBlur4.Blur(1);
          bmpBlur1.Save(pathFileNameB1);
          bmpBlur2.Save(pathFileNameB2);
          bmpBlur3.Save(pathFileNameB3);
          bmpBlur4.Save(pathFileNameB4);

        }
        catch (Exception exception)
        {
          Console.WriteLine(exception.ToString());
        }
      }
    }

    public static void TestCNN()
    {
        ImageHelper imageHelper = null;
        Stream stream = null;

        String sourceFolder = @"C:\DeepLearningImageTests\DeepLearningImageData\Validation";
        String[] files = Directory.GetFiles(sourceFolder,"*.jpg");

        foreach(String file in files)
        {
          imageHelper=new ImageHelper();
          imageHelper.LoadImage(file);
          stream = imageHelper.ToStream();
          CNNClient cnnClient = new CNNClient("http://10.0.0.73:5000");
          String result = cnnClient.Predict(CNNClient.Model.vgg16,stream);
          Console.WriteLine(String.Format("Result:{0} File:{1}",result,file));
        }
    }

    public static void ProcessImage()
    {
      ImageHelper imageHelper=new ImageHelper();
      imageHelper.LoadImage(@"C:\DeepLearningImageTests\DeepLearningImageData\RawData0\00de4729-6aa9-465e-906b-4c92bc24f7a9.jpg");
      Stream stream = imageHelper.ToStream();
      CNNClient cnnClient = new CNNClient("http://10.0.0.73:5000");
      Stream result = cnnClient.ProcessImage(stream);
      imageHelper.LoadImage(stream);
      imageHelper.Save(@"c:\2\image.jpg");
    }

    public static void CreateValidationImages(String sourcePath, String destinationPath)
    {
      String[] files = Directory.GetFiles(sourcePath,"*.jpg");
      foreach(String file in files)
      {
        Console.WriteLine($"Processing {file}");
        ImageHelper imageHelper=new ImageHelper();
        imageHelper.LoadImage(file);
        imageHelper.ToGrayScale();
        imageHelper.Resize(128,128);
        Stream stream = imageHelper.ToStream();
        CNNClient cnnClient = new CNNClient("http://10.0.0.73:5000");
        Stream processed = cnnClient.ProcessImage(stream);
        imageHelper.LoadImage(processed);
        String pureFileName = Path.GetFileName(file);
        String saveFileName = destinationPath + @"\" + pureFileName;
        imageHelper.Save(saveFileName);
      }
    }

    public static void ProcessImages(String sourcePath, String destinationPath)
    {
      String[] files = Directory.GetFiles(sourcePath,"*.jpg");
      foreach(String file in files)
      {
        Console.WriteLine($"Processing {file}");
        ImageHelper imageHelper=new ImageHelper();
        imageHelper.LoadImage(file);
        imageHelper.ToGrayScale();
        imageHelper.Resize(128,128);
        Stream stream = imageHelper.ToStream();
        CNNClient cnnClient = new CNNClient("http://10.0.0.73:5000");
        Stream processed = cnnClient.ProcessImage(stream);
        imageHelper.LoadImage(processed);
        String pureFileName = Path.GetFileName(file);
        String saveFileName = destinationPath + @"\" + pureFileName;
        imageHelper.Save(saveFileName);
      }
    }



    static void Main(string[] args)
    {
      //        ProcessImages(@"C:\Data\0",@"C:\DeepLearningImageTests\DeepLearningImageData\Data\0");
      //        ProcessImages(@"C:\Data\1",@"C:\DeepLearningImageTests\DeepLearningImageData\Data\1");


      //        GenerateImageData();
      //        TestCNN();
      //        ProcessImage();
      //        CreateValidationImages(@"C:\2",@"C:\3");

    }
  }
}
