use strict;
use warnings;

BEGIN {
    use lib 't/';
    use TestBase;
    config();
    db_create();
}

use App::RPi::EnvUI::API;
use App::RPi::EnvUI::DB;
use Data::Dumper;
use Test::More;

my $api = App::RPi::EnvUI::API->new(
    testing => 1,
    config_file => 't/envui.json'
);

my $db = App::RPi::EnvUI::DB->new(testing => 1);

{ # default state, and on time not yet reached

    # light should be off by default

    is
        $api->_config_light('on_time'),
        0,
        "light on_time is zero";

    is
        $api->_config_light('off_time'),
        0,
        "light off_time is zero";

    is
        $api->aux_state( $api->_config_control( 'light_aux' ) ),
        0,
        "light aux is currently in state off";

    my %conf = (
        now => time() + 86400,
        on_hours => '1',
    );

    $api->action_light(%conf);

    # light should be off

    is
        $api->aux_state( $api->_config_control( 'light_aux' ) ),
        0,
        "light on time not reached, light is off";
}

{ # on_since

    # light should be off for the test

    is
        $api->aux_state( $api->_config_control( 'light_aux' ) ),
        0,
        "light aux is currently in state off for the test";
}

{ # on_hours == 0 and == 24

    my $aux = $api->_config_control('light_aux');

    # 24

    $db->update('light', 'value', 24, 'id', 'on_hours');
    $api->action_light;
    is $api->aux_state($aux), 1, "when on_hours is 24, light goes on";
    is $App::RPi::EnvUI::API::pm_sub->called, 1, "pin_mode() called";
    is $App::RPi::EnvUI::API::wp_sub->called, 1, "write_pin() called";

    $App::RPi::EnvUI::API::pm_sub->reset;
    $App::RPi::EnvUI::API::wp_sub->reset;

    is $App::RPi::EnvUI::API::pm_sub->called, 0, "pin_mode() reset";
    is $App::RPi::EnvUI::API::wp_sub->called, 0, "write_pin() reset";

    # 24 state on

    $db->update('light', 'value', 24, 'id', 'on_hours');
    $api->action_light;
    is $api->aux_state($aux), 1, "when on_hours is 24, light goes on";
    is $App::RPi::EnvUI::API::pm_sub->called, 0, "pin_mode() not called if 24 hrs and state";
    is $App::RPi::EnvUI::API::wp_sub->called, 0, "write_pin() not called if 24 hrs and state";

    $App::RPi::EnvUI::API::pm_sub->reset;
    $App::RPi::EnvUI::API::wp_sub->reset;

    is $App::RPi::EnvUI::API::pm_sub->called, 0, "pin_mode() reset";
    is $App::RPi::EnvUI::API::wp_sub->called, 0, "write_pin() reset";

    # $api->aux_state($aux, 0);

    $db->update('light', 'value', 0, 'id', 'on_hours');
    $api->action_light;
    is $api->aux_state($aux), 0, "when on_hours is 0, light goes off if on";
    is $App::RPi::EnvUI::API::pm_sub->called, 1, "pin_mode() *is* called";
    is $App::RPi::EnvUI::API::wp_sub->called, 1, "write_pin() *is* called";

    $App::RPi::EnvUI::API::pm_sub->reset;
    $App::RPi::EnvUI::API::wp_sub->reset;

    is $App::RPi::EnvUI::API::pm_sub->called, 0, "pin_mode() reset";
    is $App::RPi::EnvUI::API::wp_sub->called, 0, "write_pin() reset";

    $api->aux_state($aux, 0);

    $api->action_light;
    is $api->aux_state($aux), 0, "when on_hours is 0, light stays off";
    is $App::RPi::EnvUI::API::pm_sub->called, 0, "pin_mode() *not* called";
    is $App::RPi::EnvUI::API::wp_sub->called, 0, "write_pin() *not* called";

    $db->update('light', 'value', 12, 'id', 'on_hours');
    is $api->_config_light('on_hours'), 12, "on_hours reset back to default ok";
}

unconfig();
db_remove();
done_testing();

