//
// Created by gigaw on 4/4/2023.
//

#include "../include/HealthComponent.h"

HealthComponent::HealthComponent(int totalHealth) {
    if (totalHealth < 0) {
        totalHealth = 0;
    }
    m_totalHealth = m_currentHealth = totalHealth;
}

HealthComponent::~HealthComponent() {

}

void HealthComponent::Update(SDL_Event r, int frame, std::vector<std::string> broadcastList) {
    ParseAndUpdateBroadcast(broadcastList);
}

// Maybe render health bar? Might assume too much / promote coupling
void HealthComponent::Render(SDL_Renderer *ren) {

}

void HealthComponent::ShowComponentEditor(bool* showEditor){
    ImGuiWindowFlags window_flags = 0;
    window_flags |= ImGuiWindowFlags_MenuBar;
    window_flags |= ImGuiWindowFlags_NoCollapse;

    if(ImGui::Begin("Editing Health Component", showEditor, window_flags)){
        ImGui::InputInt("Total Health", &m_totalHealth);
        ImGui::End();
    }
}

void HealthComponent::SetHealth(int newHealth) {
    m_currentHealth = newHealth;
    if (m_currentHealth > m_totalHealth) {
        m_totalHealth = m_currentHealth;
    }
}

void HealthComponent::UpdateHealth(int changeInHealth) {
    m_currentHealth += changeInHealth;
    if (m_currentHealth < 0) {
        m_currentHealth = 0;
    }
    if (m_currentHealth > m_totalHealth) {
        m_currentHealth = m_totalHealth;
    }
}
