#include "step6.h"

__attribute__((unused))
static const char *TAG = "step6";

static uint8_t cut_wires = 0;

void step6(void) {
    get_cut_wires();
    clear_all_pressed_released();

    WireColor wires[NUM_WIRES];
    load_wires_from_sd_card(wires);
    bool solution[NUM_WIRES] = {0};
    solve_wires(wires, solution);

    while (1) {
        uint8_t new_cut_wires = get_cut_wires();
        uint8_t just_cut_wires = new_cut_wires & ~cut_wires;
        cut_wires |= new_cut_wires;

        for (int i = 0; i < NUM_WIRES; i++) {
            if (just_cut_wires & (1<<i)) {
                if (solution[i]) {
                    play_clip_wav(MOUNT_POINT "/correct.wav", true, false, 3, 0);
                } else {
                    strike("Cut wrong wire");
                }
            }
        }

        if (get_touch_pressed()) {
            bool correct = true;
            for (int i = 0; i < NUM_WIRES; i++) {
                bool wire_cut = (cut_wires & (1<<i)) != 0;
                if (solution[i] && !wire_cut) {
                    correct = false;
                }
            }
            if (correct) {
                return;
            } else {
                strike("Not all wires cut!");
            }
        }

        vTaskDelay(pdMS_TO_TICKS(10));
    }
}
