#include "bpmcalculatorwidget.h"
#include "tools/bpmcalculator.h"
#include "core/editor.h"

#include <iostream>

BPMCalculatorWidget::BPMCalculatorWidget(const std::shared_ptr<BPMCalculator>& bpm_calculator, const std::shared_ptr<kku::CoreFactory>& factory) :
    Window(factory, "BPM Calculation"),
    _bpm_calculator(bpm_calculator),
    _slider(std::make_shared<BPMSlider>(factory)),
    _core_factory(factory),
    _ticked(false)
{
    _bpm_value = _core_factory->getText(kku::Font::Id::GUI);
    _bpm_value->setCharacterSize(40);
    _bpm_value->setColor(kku::Color{0, 0, 0, 255});
    _bpm_value->setString("--");
    /*_slap_buffer.loadFromFile("Tick.ogg");
    _slap.setBuffer(_slap_buffer);
    _slap.setVolume(30.);*/
}

void BPMCalculatorWidget::input(const kku::SystemEvent& event)
{
    switch (event.type)
    {
    default:
        break;

    case kku::SystemEvent::Type::KeyPress:
    {
        if (std::get<kku::SystemEvent::Key>(event.data).view == kku::SystemEvent::Key::Code::Space)
        {
            _bpm_calculator->click(_current_time());
        }
        break;
    }
    }

    Window::input(event);
}

void BPMCalculatorWidget::update(const kku::microsec& dt)
{
    Window::update(dt);

    const auto beat_info = _bpm_calculator->fetchApproximatedInfo();
    if (beat_info.BPM != 0)
    {
        _bpm_value->setString(std::to_string(static_cast<int>(beat_info.BPM)));

        const kku::microsec until_beat = _bpm_calculator->fetchTimeUntilNextBeat(_current_time());
        const auto time_relation = static_cast<long double>(beat_info.interval) / static_cast<long double>(until_beat);
        const auto slider_path_left = _slider->getRect().width / time_relation;
        if (slider_path_left < 50)
        {
            //if (!_ticked)
                //_slap.play();
            _ticked = true;
        }
        else
            _ticked = false;

        _slider->setTickPosition(slider_path_left);
    }
}

void BPMCalculatorWidget::display() const
{
    Window::display();

    if (_is_visible)
    {
        _slider->display();
        _button_start->display();
        _button_stop->display();
        _button_apply->display();
        _bpm_value->display();
    }
}

void BPMCalculatorWidget::setRect(const kku::Area<float>& rect)
{
    Window::setRect(rect);
    _slider->setRect(kku::Area<float>{0, 0, rect.width / 8 * 6, 100});
    _slider->setPosition(kku::Point{_window_content->getRect().left + rect.width / 8,
                                    _window_content->getRect().top + rect.height / 8 * 3});

    _button_start->setRect(kku::Area<float>{0, 0, rect.width / 10 * 3, 30});
    _button_start->setPosition(kku::Point{_window_content->getRect().left + rect.width / 7,
                                          _window_content->getRect().top + _window_content->getRect().height - 40});

    _button_stop->setRect(kku::Area<float>{0, 0, rect.width / 10 * 3, 30});
    _button_stop->setPosition(kku::Point{_window_content->getRect().left + rect.width / 7,
                                         _window_content->getRect().top + _window_content->getRect().height - 40});

    _button_apply->setRect(kku::Area<float>{0, 0, rect.width / 10 * 3, 30});
    _button_apply->setPosition(kku::Point{_window_content->getRect().left + 50 + (2 * (rect.width / 7)),
                                _window_content->getRect().top + _window_content->getRect().height - 40});

    _bpm_value->setPosition(kku::Point{_window_content->getRect().left + rect.width / 8,
                                       _window_content->getRect().top + rect.height / 8 });
}

void BPMCalculatorWidget::move(const kku::Vector2<float>& delta)
{
    Window::move(delta);

    _slider->move(delta);
    _bpm_value->move(delta);
}

void BPMCalculatorWidget::setPosition(const kku::Point& position)
{
    Window::setPosition(position);
}

void BPMCalculatorWidget::init(Init &&init)
{
    _button_start = init.start;
    _button_stop = init.stop;
    _button_apply = init.apply;

    _current_time = init.current_time;

    addChild(_button_start);
    addChild(_button_stop);
    addChild(_button_apply);

    _button_stop->setVisibility(false);
}

void BPMCalculatorWidget::setVisibility(bool is_visible)
{
    Window::setVisibility(is_visible);

    bool can_stop = _bpm_calculator->calculating();
    _button_stop->setVisibility(can_stop && is_visible);
    _button_start->setVisibility(!can_stop && is_visible);
}
