#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "unity_test_module.h"
#include "watch_registers.h"
#include "resource_detector.h"

// I rather dislike keeping line numbers updated, so I made my own macro to ditch the line number
#define MY_RUN_TEST(func) RUN_TEST(func, 0)

void watch_setUp(void){}
void watch_tearDown(void){}
//config
void test_settings_toggle_config_is_paused(void){
    uint8_t config = 0x00;
    watch_registers_toggle_config_is_paused(&config);
    TEST_ASSERT_EQUAL(0x08, config); // 0b1000
    watch_registers_toggle_config_is_paused(&config);
    TEST_ASSERT_EQUAL(0x00, config); // 0b0000
}
void test_setting_set_config_time_format(void){
    uint8_t config = 0x00;
    watch_registers_set_config_time_format(&config, TIME_HOUR_MINUTE);
    TEST_ASSERT_EQUAL(0x00, config); // 0b0000
    watch_registers_set_config_time_format(&config, TIME_HOUR_MINUTE_SECOND);
    TEST_ASSERT_EQUAL(0x01, config); // 0b0001
}
void test_setting_set_config_time_update_interval(void){
    uint8_t config = 0xAA;
    watch_registers_set_config_time_update_interval(&config, TIME_UPDATE_DISABLED);
    TEST_ASSERT_EQUAL(0xA8, config); // 0b0000
    watch_registers_set_config_time_update_interval(&config, TIME_EVERY_1_SECOND);
    TEST_ASSERT_EQUAL(0xAA, config); // 0b0010
    watch_registers_set_config_time_update_interval(&config, TIME_EVERY_30_SECONDS);
    TEST_ASSERT_EQUAL(0xAC, config); // 0b0100
    watch_registers_set_config_time_update_interval(&config, TIME_EVERY_MINUTE);
    TEST_ASSERT_EQUAL(0xAE, config); // 0b0110
}
void test_setting_get_config_settings(void){
    uint8_t config = 0x00; // 0b00000000 so time is updated, time format hour:minute, time refresh not updated
    bool is_paused;
    time_format format;
    time_update_interval interval;

    watch_registers_get_config_settings(config, &is_paused, &format, &interval);
    TEST_ASSERT_EQUAL(false, is_paused);
    TEST_ASSERT_EQUAL(TIME_HOUR_MINUTE, format);
    TEST_ASSERT_EQUAL(TIME_UPDATE_DISABLED, interval);

    watch_registers_toggle_config_is_paused(&config);
    watch_registers_get_config_settings(config, &is_paused, &format, &interval);
    TEST_ASSERT_EQUAL(true, is_paused);

    watch_registers_set_config_time_format(&config, TIME_HOUR_MINUTE_SECOND);
    watch_registers_get_config_settings(config, &is_paused, &format, &interval);
    TEST_ASSERT_EQUAL(TIME_HOUR_MINUTE_SECOND, format);

    watch_registers_set_config_time_update_interval(&config, TIME_EVERY_1_SECOND);
    watch_registers_get_config_settings(config, &is_paused, &format, &interval);
    TEST_ASSERT_EQUAL(TIME_EVERY_1_SECOND, interval);
}


void test_setting_get_config_settings2(void){
    uint8_t config = 0xAA; // 0b00000000 so time is updated, time format hour:minute, time refresh not updated
    bool is_paused;
    time_format format;
    time_update_interval interval;

    watch_registers_get_config_settings(config, &is_paused, &format, &interval);
    TEST_ASSERT_EQUAL(true, is_paused);
    TEST_ASSERT_EQUAL(0, format);
    TEST_ASSERT_EQUAL(0b01, interval);

    config = 0x55; 
    watch_registers_get_config_settings(config, &is_paused, &format, &interval);
    TEST_ASSERT_EQUAL(false, is_paused);
    TEST_ASSERT_EQUAL(1, format);
    TEST_ASSERT_EQUAL(0b10, interval);
}
//
//time
void test_time_set_hours(void){
    const uint8_t hours = 0x04; // 0b10100101, but we will set it to 0xA0 (0b10100000) to fit in the 4 bits
    uint8_t time_bits_high = 0;
    uint8_t time_bits_low = 0;

    watch_registers_set_time_hours(&time_bits_low, &time_bits_high, hours);
    TEST_ASSERT_EQUAL(0x40, time_bits_high); // 0b01000000
    TEST_ASSERT_EQUAL(0x00, time_bits_low); // 0b00000000
}
void test_time_set_minutes(void){
    const uint8_t minutes = 0x31; // 0b00 110001, but we will set it to 0x3F (0b00111111) to fit in the 6 bits
    uint8_t time_bits_high = 0;
    uint8_t time_bits_low = 0;

    watch_registers_set_time_minutes(&time_bits_low, &time_bits_high, minutes);
    TEST_ASSERT_EQUAL(0x0C, time_bits_high); // 0b0000 1100
    TEST_ASSERT_EQUAL(0x40, time_bits_low); // 0b01 000000
}
void test_time_set_seconds(void){
    const uint8_t seconds = 0x35; // 0b00 110101
    uint8_t time_bits_high = 0;
    uint8_t time_bits_low = 0;

    watch_registers_set_time_seconds(&time_bits_low, &time_bits_high, seconds);
    TEST_ASSERT_EQUAL(0x00, time_bits_high);
    TEST_ASSERT_EQUAL(0x35, time_bits_low); // 0b00 000000
}
void test_time_get_time(void){
    const uint8_t hours = 0x04; // 0b0100
    const uint8_t minutes = 0x30; // 0b00 110000
    const uint8_t seconds = 0x35; // 0b00 110101
    uint8_t time_bits_low = 0;
    uint8_t time_bits_high = 0;

    watch_registers_set_time_hours(&time_bits_low, &time_bits_high, hours);
    watch_registers_set_time_minutes(&time_bits_low, &time_bits_high, minutes);
    watch_registers_set_time_seconds(&time_bits_low, &time_bits_high, seconds);

    uint8_t h;
    uint8_t m;
    uint8_t s;
    watch_registers_get_time(time_bits_low, time_bits_high, &h, &m, &s);
    
    TEST_ASSERT_EQUAL(hours, h);
    TEST_ASSERT_EQUAL(minutes, m);
    TEST_ASSERT_EQUAL(seconds, s);
}
//
//date
void test_date_set_year(void){
    const uint8_t year = 0x20; // 0b00100000
    uint8_t date_bits_high = 0;
    uint8_t date_bits_low = 0;

    watch_registers_set_date_year(&date_bits_low, &date_bits_high, year);
    TEST_ASSERT_EQUAL(0x00, date_bits_high); // 0b00000000
    TEST_ASSERT_EQUAL(0x20, date_bits_low); // 0b00100000
}
void test_date_set_month(void){
    const uint8_t month = 0x05; // 0b0101
    uint8_t date_bits_high = 0;
    uint8_t date_bits_low = 0;

    watch_registers_set_date_month(&date_bits_low, &date_bits_high, month);
    TEST_ASSERT_EQUAL(0x02, date_bits_high); // 0b00000 010
    TEST_ASSERT_EQUAL(0x80, date_bits_low); // 0b1 0000000
}
void test_date_set_day_of_month(void){
    const uint8_t day_of_month = 0x15; // 0b10101
    uint8_t date_bits_high = 0;
    uint8_t date_bits_low = 0;

    watch_registers_set_date_day_of_month(&date_bits_low, &date_bits_high, day_of_month);
    TEST_ASSERT_EQUAL(0xA8, date_bits_high); // 0b10101 000
    TEST_ASSERT_EQUAL(0x00, date_bits_low); // 0b0
}
void test_date_get_date(void){
    const uint8_t year = 0x20; // 0b00100000
    const uint8_t month = 0x05; // 0b0101
    const uint8_t day_of_month = 0x16; // 0b10110
    uint8_t date_bits_high = 0;
    uint8_t date_bits_low = 0;

    watch_registers_set_date_year(&date_bits_low, &date_bits_high, year);
    watch_registers_set_date_month(&date_bits_low, &date_bits_high, month);
    watch_registers_set_date_day_of_month(&date_bits_low, &date_bits_high, day_of_month);

    uint8_t y;
    uint8_t m;
    uint8_t d;
    watch_registers_get_date(date_bits_low, date_bits_high, &y, &m, &d);
    
    TEST_ASSERT_EQUAL(year, y);
    TEST_ASSERT_EQUAL(month, m);
    TEST_ASSERT_EQUAL(day_of_month, d);
}

//full tests
void test_full_watch_configuration(void) {
    uint8_t config = 0x00;
    
    // Configure watch settings
    watch_registers_set_config_time_format(&config, TIME_HOUR_MINUTE_SECOND);
    watch_registers_set_config_time_update_interval(&config, TIME_EVERY_1_SECOND);
    watch_registers_toggle_config_is_paused(&config);

    // Verify all settings
    bool is_paused;
    time_format format;
    time_update_interval interval;
    watch_registers_get_config_settings(config, &is_paused, &format, &interval);

    TEST_ASSERT_EQUAL(true, is_paused);
    TEST_ASSERT_EQUAL(TIME_HOUR_MINUTE_SECOND, format);
    TEST_ASSERT_EQUAL(TIME_EVERY_1_SECOND, interval);
}
void test_full_datetime_overflow_setup(void) {
    uint8_t time_bits_low = 0;
    uint8_t time_bits_high = 0;
    uint8_t date_bits_low = 0;
    uint8_t date_bits_high = 0;

    // Set time to 11:59:59
    watch_registers_set_time_hours(&time_bits_low, &time_bits_high, 0x23);
    watch_registers_set_time_minutes(&time_bits_low, &time_bits_high, 0x40);
    watch_registers_set_time_seconds(&time_bits_low, &time_bits_high, 0x40);

    // Set date to 31/12/2127
    watch_registers_set_date_year(&date_bits_low, &date_bits_high, 0xC9);
    watch_registers_set_date_month(&date_bits_low, &date_bits_high, 0x12);
    watch_registers_set_date_day_of_month(&date_bits_low, &date_bits_high, 0x31);

    // Verify time
    uint8_t hours, minutes, seconds;
    watch_registers_get_time(time_bits_low, time_bits_high, &hours, &minutes, &seconds);
    TEST_ASSERT_EQUAL(0x0B, hours); // 11
    TEST_ASSERT_EQUAL(0x3B, minutes); // 59
    TEST_ASSERT_EQUAL(0x3B, seconds); // 59

    // Verify date
    uint8_t year, month, day;
    watch_registers_get_date(date_bits_low, date_bits_high, &year, &month, &day);
    TEST_ASSERT_EQUAL(0x7F, year); // 127
    TEST_ASSERT_EQUAL(0x0C, month); // 12
    TEST_ASSERT_EQUAL(0x1F, day); // 31
}
//
void run_watch_tests()
{
    UnityRegisterSetupTearDown(watch_setUp, watch_tearDown);
    MY_RUN_TEST(test_settings_toggle_config_is_paused);
    MY_RUN_TEST(test_setting_set_config_time_format);
    MY_RUN_TEST(test_setting_set_config_time_update_interval); 
    MY_RUN_TEST(test_setting_get_config_settings);
    MY_RUN_TEST(test_setting_get_config_settings2);

    MY_RUN_TEST(test_time_set_hours);
    MY_RUN_TEST(test_time_set_minutes);
    MY_RUN_TEST(test_time_set_seconds);
    MY_RUN_TEST(test_time_get_time);

    MY_RUN_TEST(test_date_set_year);
    MY_RUN_TEST(test_date_set_month);
    MY_RUN_TEST(test_date_set_day_of_month);
    MY_RUN_TEST(test_date_get_date);

    MY_RUN_TEST(test_full_watch_configuration);
    UnityUnregisterSetupTearDown();
}
