#CLASS {missiontimers} {kill};
#CLASS {missiontimers} {open};

#NOP ==[ Load Individual Mission stuff ]==
#READ {src/missions/sausages.tin};
#READ {src/missions/letters.tin};
#READ {src/missions/sailing.tin};
#READ {src/missions/jobmarket.tin};

#VARIABLE {missiontimers_file} {logs/features/missiontimers_sync.tin};


#ALIAS {^missions help$} {
    #showme {<acf>[<fff>missions<acf>] Mission timers help:<099>};
    #showme {Mission timers track the cooldown period for various missions across multiple characters.};
    #showme {};
    #showme {<acf>Supported mission cooldowns:<099>};
    #showme { <ffa>*<099> AM Job Market (Mr Werks)<099>};
    #showme { <ffa>*<099> Dead Letter Office (Frank)<099>};
    #showme { <ffa>*<099> Sam Slager Sausage Jobs<099>};
    #showme { <ffa>*<099> Sailing Cooldown (Chidder)<099>};
    #showme {};
    #showme {<acf>Available commands:<099>};
    #showme { <ffa>*<099> "<afe>missions<099>" - show active cooldowns (all characters)<099>};
    #showme { <ffa>*<099> "<afe>missions help<099>" - show this helpfile<099>};
    #showme { <ffa>*<099> "<afe>mtsave<099>" - save active cooldowns to file<099>};
    #showme { <ffa>*<099> "<afe>mtload<099>" - load active cooldowns from file<099>};
    #showme {};
    #showme {When a timer runs out, a message will print to inform which character and mission are ready to do again.};
    #showme {};
};

#NOP ==[ Missions command reports on each modules active/retry lists ]==;
#ALIAS {missions} {
    #if {&missions == 0} { 
        #return;
    };

    #format {current_time} {%T};
    #var {mission_display_list} {};

    #nop ==============================;
    #nop =====  Sailing Missions  =====;
    #nop ==============================;
    #if {&missions[sailing]} {
        #foreach {*missions[sailing][active_list][]} {player} {
            #var {mins}  {@ts_to_min{$current_time;$missions[sailing][active_list][$player]}};
            #math {mins} {120 - $mins};

            #math {hours} {$mins / 60};
            #math {mins} {$mins % 60};

            #if {$mins < 0} {
                #unvar {missions[sailing][active_list][$player]};
            };
            #else {
                #if {$hours <= 0} {
                    #format {mt_display} {\t<138>*<099> <acf>%s<099> can do another sailing mission in <139>%d<099> minute(s).<099>} {$player}{$mins};
                };
                #else {
                    #format {mt_display} {\t<138>*<099> <acf>%s<099> can do another sailing mission in <139>%d<099> hour and <139>%d<099> minute(s).<099>} {$player}{$hours}{$mins};
                };
                #list {mission_display_list[$player]} {add} {$mt_display};
            };
        };
    };


    #nop ==============================;
    #nop =====  Sausage Missions  =====;
    #nop ==============================;
    #if {&missions[sausage]} {
        #foreach {*missions[sausage][active_list][]} {player} {
            #if {&missions[sausage][active_list][$player][easy]} {
                #var {mins}  {@ts_to_min{$current_time;$missions[sausage][active_list][$player][easy]}};
                #math {mins} {60 - $mins};

                #format {mt_display} {\t<138>*<099> <acf>%s<099> can do another unspiced sausage mission in <139>%d<099> minutes.<099>} {$player}{$mins};
                #list {mission_display_list[$player]} {add} {$mt_display};
            };

            #if {&missions[sausage][active_list][$player][hard]} {
                #var {mins}  {@ts_to_min{$current_time;$missions[sausage][active_list][$player][hard]}};
                #math {mins} {60 - $mins};

                #format {mt_display} {\t<138>*<099> <acf>%s<099> can do another spiced sausage mission in <139>%d<099> minutes.<099>} {$player}{$mins};
                #list {mission_display_list[$player]} {add} {$mt_display};
            };
        };

        #foreach {*missions[sausage][retry_list][]} {player} {
            #var {mins}  {@ts_to_min{$current_time;$missions[sausage][retry_list][$player]}};
            #math {mins} {10 - $mins};

            #format {mt_display} {\t<138>*<099> <acf>%s<099> can request a new sausage mission in <139>%d<099> minutes.<099>} {$player}{$mins};
            #list {mission_display_list[$player]} {add} {$mt_display};
        };
    };

    #nop =================================;
    #nop =====  Job Market Missions  =====;
    #nop =================================;
    #if {&missions[jobmarket]} {
        #foreach {*missions[jobmarket][active_list][]} {player} {
            #foreach {*missions[jobmarket][active_list][$player][]} {job} {
                #var {mins}  {@ts_to_min{$current_time;$missions[jobmarket][active_list][$player][$job]}};
                #math {mins} {60 - $mins};

                #format {mt_display} {\t<138>*<099> <acf>%s<099> can do another Job Market "%c%s%c" mission in <139>%d<099> minutes.<099>} {$player}{Jade}{$job}{<099>}{$mins};
                #list {mission_display_list[$player]} {add} {$mt_display};
            };
        };

        #foreach {*missions[jobmarket][retry_list][]} {player} {
            #var {mins}  {@ts_to_min{$current_time;$missions[jobmarket][retry_list][$player]}};
            #math {mins} {10 - $mins};

            #format {mt_display} {\t<138>*<099> <acf>%s<099> can request a new Job Market mission in <139>%d<099> minutes.<099>} {$player}{$mins};
            #list {mission_display_list[$player]} {add} {$mt_display};
        };
    };


    #nop ==============================;
    #nop =====    Dead Letters    =====;
    #nop ==============================;
    #if {&missions[letter]} {
        #foreach {*missions[letter][active_list][]} {player} {
            #foreach {*missions[letter][active_list][$player][]} {difficulty} {
                #var {mins}  {@ts_to_min{$current_time;$missions[letter][active_list][$player][$difficulty]}};
                #math {mins} {60 - $mins};

                #if {$mins < 0} {
                    #unvar {missions[letter][active_list][$player][$difficulty]};
                } {
                    #format {mt_display} {\t<138>*<099> <acf>%s<099> can do another "<188><afc>%s<099>" letter in <139>%d<099> minutes.<099>} {$player}{$letter_types[$difficulty]}{$mins};
                    #list {mission_display_list[$player]} {add} {$mt_display};
                };
            };
        };
        #foreach {*missions[letter][retry_list][]} {player} {
            #var {mins}  {@ts_to_min{$current_time;$missions[letter][retry_list][$player]}};
            #math {mins} {10 - $mins};

            #if {$mins < 0} {
                #unvar {missions[letter][retry_list][player]};
            } {
                #format {mt_display} {\t<138>*<099> <acf>%s<099> can request another letter in <139>%d<099> minutes.<099>} {$player}{$mins};
                #list {mission_display_list[$player]} {add} {$mt_display};
            };
        };

    };


    #nop ==============================;
    #nop =====   Print Missions   =====;
    #nop ==============================;
    #list {mission_display_list} {size} {mdl_count};
    #if {$mdl_count > 0} {
        #echo {<acf>[<fff>missions<acf>] Active Mission Timers:<099>}; 
        #foreach {*mission_display_list[]} {player} {
            #foreach {$mission_display_list[$player][%*]} {mt_display} {
                #showme {$mt_display};
            };
        };
    } {
        #echo {<acf>[<fff>missions<acf>] No current mission timers.<099>};
    };

};

#FUNCTION {ts_to_min} {
    #nop @ts_to_min{<current_time>\;<missiontime>};
    #math {mins} {%1 - %2};
    #math {mins} {($mins - (60 - 1)) / 60};

    #return {$mins};
};

#NOP ==[ read/write from file for persistence ]==;
#ALIAS {mtsave} {
    /writemissiontimerstofile;

    #format {mtsync_message} {<acf>[<fff>missions<acf>] <138>*<900><acf> Saved all available mission timers from %cthis session%c to the disk.<099>} {Green}{<acf>};
    #echo {$mtsync_message};
};

#ALIAS {/writemissiontimerstofile} {
    #class {mtsync} {clear};
    #class {mtsync} {open};
    #var {missions_sync} {$missions};
    #class {mtsync} {close};

    #system {touch $missiontimers_file};
    #class {mtsync} {write} {$missiontimers_file};
}

#ALIAS {mtload} {
    #class {mtsync} {clear};
    #class {mtsync} {read} {$missiontimers_file};

    #nop == overwrite mission variable with loaded data;
    #variable {missions} {$missions_sync};

    #nop == recreate delays for all mission modules;
    /sausage_recreate_delays;
    /sailing_recreate_delays;
    /letters_recreate_delays;
    /jobmarket_recreate_delays;

    #format {mtsync_message} {<acf>[<fff>missions<acf>] <138>*<900><acf> Synced all available mission timers from %clast session%c off the disk.<099>} {Green}{<188><acf>};
    #echo {$mtsync_message};
};

#CLASS {missiontimers} {close};
