#include "ctrl_disp_button.h"

void doNoting(APP_data_t *app_data)
{
    return;
}

void backToMain(APP_data_t *app_data)
{
    app_data->curr_state_lay = LAY_MAIN;

    switch (app_data->curr_gen->type)
    {
    case GEN_FG_TYPE:
        app_data->curr_state_btn = BTN_MAIN_FG;
        break;

    case GEN_PWM_TYPE:
        app_data->curr_state_btn = BTN_MAIN_PWM;
        break;

    default:
        break;
    }

    // app_data->isButtonChange = 1;
    // app_data->isGraphChange = 1;
    // app_data->isValueChange = 1;
    // app_data->isGraphChange = 1;
    app_data->disp_update = UPDATE_ALL;
}

void blockFocusAtMaxAndMin(APP_data_t *app_data)
{
    switch (app_data->curr_state_btn)
    {
    case BTN_FREQ_MIN:
        if (app_data->freq_focus_digit > 0)
        {
            app_data->curr_state_btn = BTN_FREQ;
            app_data->disp_update |= UPDATE_BUTTON;
        }
        break;
    case BTN_FREQ_MAX:
        if (app_data->freq_focus_digit < FUN_GEN_FOCUS_MAX)
        {
            app_data->curr_state_btn = BTN_FREQ;
            app_data->disp_update |= UPDATE_BUTTON;
        }
        break;
    case BTN_FREQ:
        if (app_data->freq_focus_digit == 0)
        {
            app_data->curr_state_btn = BTN_FREQ_MIN;
            app_data->disp_update |= UPDATE_BUTTON;
        }
        else if (app_data->freq_focus_digit == FUN_GEN_FOCUS_MAX)
        {
            app_data->curr_state_btn = BTN_FREQ_MAX;
            app_data->disp_update |= UPDATE_BUTTON;
        }
        break;
    default:
        break;
    }
}

void enterToFreqLayout(APP_data_t *app_data)
{
    app_data->curr_state_lay = LAY_FREQ;
    app_data->curr_state_btn = BTN_FREQ;
    blockFocusAtMaxAndMin(app_data);

    // app_data->isButtonChange = 1;
    // app_data->isGraphChange = 1;
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_BUTTON | UPDATE_GRAPH | UPDATE_VALUE;
}

void enterToAmplLayout(APP_data_t *app_data)
{
    app_data->curr_state_lay = LAY_AMPL;
    app_data->curr_state_btn = BTN_AMPL;

    // app_data->isButtonChange = 1;
    // app_data->isGraphChange = 1;
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_BUTTON | UPDATE_GRAPH | UPDATE_VALUE;
}

void enterToOffslLayout(APP_data_t *app_data)
{
    app_data->curr_state_lay = LAY_OFFS;
    app_data->curr_state_btn = BTN_OFFS;

    // app_data->isButtonChange = 1;
    // app_data->isGraphChange = 1;
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_BUTTON | UPDATE_GRAPH | UPDATE_VALUE;
}

void enterToPhasLayout(APP_data_t *app_data)
{
    app_data->curr_state_lay = LAY_PHAS;
    app_data->curr_state_btn = BTN_PHAS;

    // app_data->isButtonChange = 1;
    // app_data->isGraphChange = 1;
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_BUTTON | UPDATE_GRAPH | UPDATE_VALUE;
}

void enterToDutyLayout(APP_data_t *app_data)
{
    app_data->curr_state_lay = LAY_DUTY;
    app_data->curr_state_btn = BTN_DUTY;

    // app_data->isButtonChange = 1;
    // app_data->isGraphChange = 1;
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_BUTTON | UPDATE_GRAPH | UPDATE_VALUE;
}

void enterToWavelLayout(APP_data_t *app_data)
{
    app_data->curr_state_lay = LAY_WAVE;
    app_data->curr_state_btn = BTN_WAVE;

    // app_data->isButtonChange = 1;
    // app_data->isGraphChange = 1;
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_BUTTON | UPDATE_GRAPH | UPDATE_VALUE;
}

void moveToLeftFocusFreqNumber(APP_data_t *app_data)
{
    app_data->freq_focus_digit += 1;
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_VALUE;

    blockFocusAtMaxAndMin(app_data);
    app_data->timer_blink[app_data->last_key] = 2;
    app_data->button_blink |= (1 << app_data->last_key);
}

void moveToRighttFocusFreqNumber(APP_data_t *app_data)
{
    app_data->freq_focus_digit -= 1;
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_VALUE;

    blockFocusAtMaxAndMin(app_data);
    app_data->timer_blink[app_data->last_key] = 2;
    app_data->button_blink |= (1 << app_data->last_key);
}

void setTo0_01xFocusNumber(APP_data_t *app_data)
{
    switch (app_data->curr_state_lay)
    {
    case LAY_AMPL:
        app_data->ampl_focus_digit = 0;
        break;
    case LAY_OFFS:
        app_data->offs_focus_digit = 0;
        break;
    default:
        break;
    }

    app_data->timer_blink[app_data->last_key] = 2;
    app_data->button_blink |= (1 << app_data->last_key);
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_VALUE;
}
void setTo0_1xFocusNumber(APP_data_t *app_data)
{
    switch (app_data->curr_state_lay)
    {
    case LAY_AMPL:
        app_data->ampl_focus_digit = 1;
        break;
    case LAY_OFFS:
        app_data->offs_focus_digit = 1;
        break;
    default:
        break;
    }

    app_data->timer_blink[app_data->last_key] = 2;
    app_data->button_blink |= (1 << app_data->last_key);
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_VALUE;
}
void setTo1xFocusNumber(APP_data_t *app_data)
{
    switch (app_data->curr_state_lay)
    {
    case LAY_AMPL:
        app_data->ampl_focus_digit = 2;
        break;
    case LAY_OFFS:
        app_data->offs_focus_digit = 2;
        break;
    case LAY_PHAS:
        app_data->phas_focus_digit = 0;
        break;
    case LAY_DUTY:
        app_data->duty_focus_digit = 0;
        break;
    default:
        break;
    }

    app_data->timer_blink[app_data->last_key] = 2;
    app_data->button_blink |= (1 << app_data->last_key);
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_VALUE;
}
void setTo10xFocusNumber(APP_data_t *app_data)
{
    switch (app_data->curr_state_lay)
    {
    case LAY_AMPL:
        app_data->ampl_focus_digit = 3;
        break;
    case LAY_OFFS:
        app_data->offs_focus_digit = 3;
        break;
    case LAY_PHAS:
        app_data->phas_focus_digit = 1;
        break;
    case LAY_DUTY:
        app_data->duty_focus_digit = 1;
        break;
    default:
        break;
    }

    app_data->timer_blink[app_data->last_key] = 2;
    app_data->button_blink |= (1 << app_data->last_key);
    // app_data->isValueChange = 1;
    app_data->disp_update |= UPDATE_VALUE;
}
