#
module ^tutorial
#
#
function &next_step($step) {
    var $tut_on, $next;
    char MC;
    cont $tkeys[];
    $tut_on = &config_value("run_tutorial");
    if($tut_on) {
        MC = &get_mc();
        if($step == "") {
            $step = &get_flag(MC, "tutorial");
            if($step == "") {
                $step = "start";
            }
        }
        if($step != "end") {
            $tkeys = &split($step, ".");
            if($tkeys[0] == "start") {
                $next = &tutorial_start(MC);
            }
            elsif($tkeys[0] == "main_ui") {
                $next = &tutorial_main_ui(MC, $tkeys[1]);
            }
            elsif($tkeys[0] == "map") {
                $next = &tutorial_map(MC, $tkeys[1]);
            }
            elsif($tkeys[0] == "club") {
                $next = &tutorial_club(MC, $tkeys[1]);
            }
            elsif($tkeys[0] == "contacts") {
                $next = &tutorial_contacts(MC, $tkeys[1]);
            }
            elsif($tkeys[0] == "city") {
                $next = &tutorial_city(MC, $tkeys[1]);
            }
            &set_flag(MC, "tutorial", $next, 0);
        }
    }
    return $next;
}


function &end_tutorial(A) {
    &set_flag(A, "tutorial", "end", 0);
    return;
}

function &tutorial_series(A, $title_key, $key_lists) {
    var $ans, $title, $body;
    cont $body_keys[];
    $title = &localize("tut." ^ $title_key ^ ".t");
    $body_keys = &split($key_lists, ",");
    foreach $bk in $body_keys {
        $body = &localize("tut." ^ $bk ^ ".b");
        $ans = decision A  {
            title "var"~$title
            body "var"~$body
            choice "ok", 1, "ok"
        }
    }
    return;
}



function &tutorial_start(A) {
    var $ans, $which;
    $ans = decision A {
        title "tut.start.t"
        body "tut.start1.b"
        choice "ok", 1, "ok"
    }

    $ans = decision A {
        title "tut.start.t"
        body "tut.start2.b"
        choice "opt1", 1, "tut.start2.opt.1"
        choice "opt2", 1, "tut.start2.opt.2"
    }

    if($ans == "opt1") {
        $which = "Option 1";
    }
    else {
        $which = "Option 2";
    }
    $ans = decision A {
        title "tut.start.t"
        body "tut.start3.b"~$which
        choice "ok", 1, "tut.start3.opt.hint"
        choice "no", 1, "tut.start3.opt.no"
        hint "tut.start3.hint"
    }

    $ans = decision A {
        title "tut.start.t"
        body "tut.start4.b"
        choice "ok", 1, "letsgo"
    }
    return "main_ui.top";
}

function &tutorial_main_ui(A, $substep) {
    var $ans, $ns, $days;
    cont %clubs[];
    char O1, O2;
    $days = 1;
    if($substep == "top") {
        &tutorial_series(A, "main_ui", "main_ui,main_ui.top,main_ui.top.mc,main_ui.top.mc2,main_ui.top.mc3,main_ui.top.mc4,main_ui.top.mc5,main_ui.top.mc6,main_ui.top.club,main_ui.top.help,main_ui.top.left,main_ui.top.end");
        $ns = "main_ui.menu";
    }
    elsif($substep == "menu") {
        &tutorial_series(A, "main_ui", "main_ui.menu1,main_ui.menu2,main_ui.menu3,main_ui.menu4");
        $ns = "main_ui.actions";
    }
    elsif($substep == "actions") {
        &tutorial_series(A, "main_ui", "main_ui.actions1,main_ui.actions2,main_ui.actions3,main_ui.actions4,main_ui.actions5");
        $ns = "main_ui.interactions";
    }
    elsif($substep == "interactions") {
        &tutorial_series(A, "main_ui", "main_ui.interactions1,main_ui.interactions2,main_ui.interactions3");

        %clubs = &all_clubs(false);
        foreach %c in %clubs {
            if(%c != A.club) {
                debug "Checking club " ^ %c.name;
                if(!O1) {
                    O1 = %c.owner;
                    debug "Adding O1 " ^ O1.name;
                    &set_flag_ticks(O1, "keep_here", O1.loc_id, 1);
                    &join(O1, A);
                }
                else {
                    O2 = %c.owner;
                    debug "Adding O2 " ^ O2.name;
                    &set_flag_ticks(O2, "keep_here", O2.loc_id, 1);
                    &join(O2, A);
                    break;
                }
            }
        }
        &tutorial_series(A, "main_ui", "main_ui.interactions4,main_ui.interactions5");
        $days = 3;
        &set_flag(A, "tut_inter", "yes", 4);
        $ns = "main_ui.msglog";
    }
    elsif($substep == "msglog") {
        &tutorial_series(A, "main_ui", "main_ui.msglog1,main_ui.msglog2,main_ui.msglog3");
        $ns = "map.city";
    }
    sched $days days, 1, &tutorial::next_step($ns);
    return $ns;
}


action &tutorial_action(A)
    details "Continue Tutorial", 1, 1, 99, "not_ai in_tutorial"
    init {
        var $allow_it, $fv;
        obj %room;
        $fv = &get_flag(A, "tut_act");
        if($fv != "") {
            if($fv == "park") {
                if(A.loc_type == "park") {
                    $allow_it = true;
                }
            }
            elsif($fv == "bar") {
                if(A.is_at_work) {
                    %room = A.room_at;
                    if(%room) {
                        if(%room.type_key == "bar") {
                            $allow_it = true;
                        }
                    }
                }
            }
            elsif($fv == "bar2") {
                if(A.is_at_work) {
                    $allow_it = true;
                }
            }
        }
    }
    allow $allow_it;
    aiprob 100;
{
    $fv = &get_flag(A, "tut_act");
    if($fv == "park") {
        &tutorial_series(A, "map", "map.park1,map.park2,map.park3,map.park4,map.park5");
        &set_flag(A, "tut_act", "", -1);
    }
    elsif($fv == "bar") {
        &tutorial_series(A, "club", "club.bar1,club.bar2,club.bar3,club.bar4,club.bar5,club.bar6");
        &set_flag(A, "tut_act", "bar2", 3);
    }
    elsif($fv == "bar2") {
        &tutorial_series(A, "club", "club.hiring1,club.hiring2,club.hiring3");
        &set_flag(A, "tut_act", "", -1);
    }
    return;
}


interaction &tutorial_interaction(A, T)
    details "Tutorial Talk", 1, 1, 99, "not_ai in_tutorial"
    init {
        var $allow_it, $fv;
        $fv = &get_flag(A, "tut_inter");
        if($fv != "") {
            $allow_it = true;
        }
    }
    allow $allow_it;
    aiprob 100;
{
    var $ans, $known;
    char X;

    $known = &have_relation(A,T);
    $ans = decision A, T {
        title "tut.inter.t"
        body "tut.inter1.b"
        bodyif $known, "tut.inter1.known.b"
        bodyif ! $known, "tut.inter1.not_known.b"
        choice "ok", 1, "ok"
    }

    if(!$known) {
        &introductions::do_intro(A,T);
        $ans = decision A, T {
            title "tut.inter.t"
            body "tut.inter2.b"
            choice "ok", 1, "ok"
        }
    }

    $ans = decision A, T {
        title "tut.inter.t"
        body "tut.inter3.b"
        choice "ok", 1, "ok"
    }

    $ans = decision A, T {
        title "tut.inter.t"
        body "tut.inter4.b"
        choice "ok", 1, "ok"
    }

    $ans = decision A, T {
        title "tut.inter.t"
        body "tut.inter5.b"
        choice "ok", 1, "ok"
    }

    X = A.closest_friend;
    &join(X, A);
    $ans = decision A, T, X {
        title "tut.inter.t"
        body "tut.inter.two.b"
        choice "ok", 1, "ok"
    }

    $ans = decision A, -, X {
        title "tut.inter.t"
        body "tut.inter.left.b"
        choice "ok", 1, "ok"
    }

    $ans = decision A, -, -, X {
        title "tut.inter.t"
        body "tut.inter.bot.b"
        choice "ok", 1, "ok"
    }

    $ans = decision A, T, -, X {
        title "tut.inter.t"
        body "tut.inter.about.b"
        choice "ok", 1, "ok"
    }

    $ans = decision A, T, X, X, T, X {
        title "tut.inter.t"
        body "tut.inter.other.b"
        choice "ok", 1, "ok"
    }

    &set_flag(A, "tut_inter", "", -1);
    return;
}

function &tutorial_map(A, $substep) {
    var $ns, $days;
    $days = 1;
    if($substep == "city") {
        &tutorial_series(A, "map", "map.city1,map.city2,map.city3,map.city4,map.city5,map.city6");
        &set_flag(A, "tut_act", "park", 2);
        $ns = "map.club";
    }
    elsif($substep == "club") {
        if(A.is_at_work) {
            &tutorial_series(A, "club", "club1,club2,club3,club4,club5,club6");
            &set_flag(A, "tut_act", "bar", 3);
            $days = 3;
            $ns = "club.interview";
        }
        else {
            $ns = "map.club";
        }
    }
    sched $days days, 1, &tutorial::next_step($ns);
    return $ns;
}

function &tutorial_club(A, $substep) {
    obj %club;
    var $ok, $ns, $days, $ans, $steps;
    %club = A.club;
    if($substep == "interview") {
        $steps = "";
        if(%club.employees > 1) {
            $ok = 2;
            $steps = "interview.hired,";
        }
        elsif(%club.jobs_posted) {
            $ok = 1;
        }
        else {
            $ok = 0;
        }
        if($ok > 0) {
            $steps = $steps ^ "interview.posted,interview2,interview3,interview4,interview5";
            if($ok < 2) {
                $steps = $steps ^ ",interview6";
            }
            &tutorial_series(A, "club", $steps);
            $ns = "club.open";
        }
        else {
            $ns = "interview";
            $days = 3;
        }
    }
    elsif($substep == "open") {
        $ans = decision A  {
            title "tut.club.t"
            body "tut.club.open.b"
            choice "no_inv", 1, "tut.club.open.opt.no_inv"
            choice "no_bt", 1, "tut.club.open.opt.no_bt"
            choice "yes", 1, "tut.club.open.opt.yes"
        }

        if($ans == "no_inv") {
            $ans = decision A {
                title "tut.club.t"
                body "tut.club.open.no_inv.b"
                choice "yes", 1, "yes"
                choice "no", 1, "no"
            }
            if($ans == "yes") {
                set $ans = decision A {
                    title "tut.club.t"
                    body "tut.club6.b"
                    choice "ok", 1, "ok"
                }
                &set_flag(A, "tut_act", "bar", 3);
                $days = 3;
                $ns = "club.open";
            }
        }
        elsif($ans == "no_bt") {
            $ans = decision A {
                title "tut.club.t"
                body "tut.club.open.no_bt.b"
                choice "yes", 1, "yes"
                choice "no", 1, "no"
            }
            if($ans == "yes") {
                set $ans = decision A {
                    title "tut.club.t"
                    body "tut.club6.b"
                    choice "ok", 1, "ok"
                }
                &set_flag(A, "tut_act", "bar2", 3);
                $days = 3;
                $ns = "club.open";
            }
        }
        else {
            &tutorial_series(A, "club", "club.open1,club.open2,club.open3,club.open4,club.open5");
            $ns = "club.kitch";
            $days = 2;
        }
        if($ans == "no") {
            set $ans = decision A {
                title "tut.club.t"
                body "tut.club.open.no.no.b"
                choice "ok", 1, "ok"
            }
            $days = 5;
            $ns = "club.open";
        }
    }
    elsif($substep == "kitch") {
        if(%club.operating) {
            &tutorial_series(A, "club", "club.kitch1,club.kitch2,club.kitch3,club.kitch4");
            $days = 2;
            $ns = "club.money";
        }
        else {
            $days = 3;
            $ns = "club.kitch";
        }
    }
    elsif($substep == "money") {
        &tutorial_series(A, "club", "money1,money2,money3,money4,money5,money6,money7,money8");
        $days = 2;
        $ns = "contacts.intro";
    }
    elsif($substep == "adv") {
        &tutorial_series(A, "club.adv", "club.adv1,club.adv2,club.adv3");
        $days = 3;
        $ns = "club.pol";
    }
    elsif($substep == "pol") {
        &tutorial_series(A, "club.adv", "club.pol1,club.pol2,club.pol3,club.pol4,club.pol5,club.pol6,club.pol7,club.pol8");
        $days = 3;
        $ns = "club.stage";
    }
    elsif($substep == "stage") {
        &tutorial_series(A, "club.adv", "club.stage1,club.stage2,club.stage3,club.stage4,club.stage5");
        $days = 3;
        $ns = "city.commish";
    }

    sched $days days, 1, &tutorial::next_step($ns);
    return $ns;
}


function &tutorial_contacts(A, $substep) {
    var $ans, $ns, $days;
    char X;
    if($substep == "intro") {
        &tutorial_series(A, "contacts", "contacts.intro1,contacts.intro2,contacts.intro3,contacts.intro4,contacts.intro5");
        $days = 2;
        $ns = "contacts.details";
    }
    elsif($substep == "details") {
        X = A.closest_friend;
        $ans = decision A, X {
            title "tut.contacts.t"
            body "tut.contacts.det1.b"
            choice "ok", 1, "ok"
        }
        $ans = decision A, X {
            title "tut.contacts.t"
            body "tut.contacts.det2.b"
            choice "ok", 1, "ok"
        }
        $ans = decision A, X {
            title "tut.contacts.t"
            body "tut.contacts.det3.b"
            choice "ok", 1, "ok"
        }
        $ans = decision A, X {
            title "tut.contacts.t"
            body "tut.contacts.det4.b"
            choice "ok", 1, "ok"
        }
        $ans = decision A, X {
            title "tut.contacts.t"
            body "tut.contacts.det5.b"
            choice "ok", 1, "ok"
        }
        $ans = decision A, X {
            title "tut.contacts.t"
            body "tut.contacts.det6.b"
            choice "ok", 1, "ok"
        }
        $days = 3;
        $ns = "club.adv";
    }

    sched $days days, 1, &tutorial::next_step($ns);
    return $ns;
}


function &tutorial_city(A, $substep) {
    var $ns;
    if($substep == "commish") {
        &tutorial_series(A, "city.commish", "city.commish1,city.commish2,city.commish3,city.commish4,city.commish5,city.commish6");
        $ns = "city.hints";
        sched 3 days, 1, &tutorial::next_step($ns);
    }
    if($substep == "hints") {
        &tutorial_series(A, "hints", "city.commish1,city.commish2,city.commish3,city.commish4,city.commish5,city.commish6");
        $ns = "";
        &end_tutorial(A);
    }
    return $ns;
}


