/*
  ==============================================================================

   This file is part of the JUCE examples.
   Copyright (c) 2022 - Raw Material Software Limited

   The code included in this file is provided under the terms of the ISC license
   http://www.isc.org/downloads/software-support-policy/isc-license. Permission
   To use, copy, modify, and/or distribute this software for any purpose with or
   without fee is hereby granted provided that the above copyright notice and
   this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
   WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
   PURPOSE, ARE DISCLAIMED.

  ==============================================================================
*/

/*******************************************************************************
 The block below describes the properties of this PIP. A PIP is a short snippet
 of code that can be read by the Projucer and used to generate a JUCE project.

 BEGIN_JUCE_PIP_METADATA

 name:             HelloWorldDemo
 version:          1.0.0
 vendor:           JUCE
 website:          http://juce.com
 description:      Simple HelloWorld application.

 dependencies:     juce_core, juce_data_structures, juce_events, juce_graphics,
                   juce_gui_basics
 exporters:        xcode_mac, vs2022, linux_make, xcode_iphone

 moduleFlags:      JUCE_STRICT_REFCOUNTEDPOINTER=1

 type:             Component
 mainClass:        HelloWorldDemo

 useLocalCopy:     1

 END_JUCE_PIP_METADATA

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

#pragma once

#include <libch341eeprom.h>

//==============================================================================
class HelloWorldDemo  : public Component, public juce::FilenameComponentListener
{
public:
    int fv1flash_write(std::string &filename) {
        ch341_operation_desc opdesc{};
        opdesc.operation = 'w';
        opdesc.filename = strdup(filename.data());
        opdesc.eepromname = strdup("24c32");
                 
        return ch341_operation(opdesc);
    }

    int fv1flash_erase() {
        ch341_operation_desc opdesc{};
        opdesc.operation = 'e';
        opdesc.eepromname = strdup("24c32");
                 
        return ch341_operation(opdesc);
    }


    //==============================================================================
    HelloWorldDemo()
    {
        fileComp.reset (new juce::FilenameComponent ("fileComp",
                                                     {},                       // current file
                                                     false,                    // can edit file name,
                                                     false,                    // is directory,
                                                     false,                    // is for saving,
                                                     {},                       // browser wildcard suffix,
                                                     {},                       // enforced suffix,
                                                     "Drop fv-1 program \n\nHere\n\nto upload (bin file)"));  // text when nothing selected
        addAndMakeVisible (fileComp.get());
        fileComp->addListener (this);

        addAndMakeVisible (helloWorldLabel);

        helloWorldLabel.setFont (Font (40.00f, Font::bold));
        helloWorldLabel.setJustificationType (Justification::centred);
        helloWorldLabel.setEditable (false, false, false);
        helloWorldLabel.setColour (Label::textColourId, Colours::green);

        addAndMakeVisible (status_label);

        status_label.setFont (Font (20.00f, Font::plain));
        status_label.setJustificationType (Justification::left);
        status_label.setEditable (false, false, false);
        status_label.setColour (Label::textColourId, Colours::white);

        addAndMakeVisible (helloWorldLabel2);

        helloWorldLabel2.setFont (Font (12.00f, Font::plain));
        helloWorldLabel2.setJustificationType (Justification::right);
        helloWorldLabel2.setEditable (false, false, false);
        helloWorldLabel2.setColour (Label::textColourId, Colours::white);


        addAndMakeVisible (quitButton);
        quitButton.onClick = [this]{
            if (fv1flash_erase() == 0) {
                status_label.setColour (Label::textColourId, Colours::green);
                status_label.setText("Erase Successful", juce::NotificationType::dontSendNotification);
            } else {
                status_label.setColour (Label::textColourId, Colours::red);
                status_label.setText("Erase Error: fv-1 board not found)", juce::NotificationType::dontSendNotification);
            }
        };

        setSize (500, 400);
    }

    //==============================================================================
    void paint (Graphics& g) override
    {
        g.fillAll (Colour (0,0,0));

    }

    void resized() override
    {
        helloWorldLabel.setBounds (10, 10, getWidth()- 10, 60);
        fileComp->setBounds(30, 100, getWidth() - 60, 200);
        status_label.setBounds(25, 290, getWidth()- 10, 60);
        helloWorldLabel2.setBounds(10, 360, 380, 30);
        quitButton.setBounds (350, 307, 120, 32);

    }
    
    void filenameComponentChanged (juce::FilenameComponent* fileComponentThatHasChanged) override
    {
        if (fileComponentThatHasChanged == fileComp.get()) {
            std::string filename = fileComp->getCurrentFile().getFullPathName().toStdString();
            status_label.setText("Uploading...", juce::NotificationType::dontSendNotification);
            if (fv1flash_write(filename) == 0) {
                status_label.setColour (Label::textColourId, Colours::green);
                status_label.setText("Upload Successful", juce::NotificationType::dontSendNotification);
            } else {
                status_label.setColour (Label::textColourId, Colours::red);
                status_label.setText("Upload Error: fv-1 board not found)", juce::NotificationType::dontSendNotification);
            }
        }
    }



private:
    //==============================================================================
    std::unique_ptr<juce::FilenameComponent> fileComp;
    Label helloWorldLabel { {}, TRANS("FlipFloater FV-1 Fun") };
    Label helloWorldLabel2 { {}, TRANS("made by <heck@heck.live> @ homemade23 4 FlipFloater") };
    Label status_label { {}, TRANS("") };
    TextButton quitButton { TRANS("Erase") };
    

    //==============================================================================
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HelloWorldDemo)
};