#
module ^security
#
# A security room worker spies on another worker
# (a manager may do this too!)
action &spy_on_workers(A)
    details "Spy On Worker >", 15, 7, -1, "at_work club_emp not_tired ai_only"
    init {
        var $allow_it, $ai_prob;
        char O;
        obj %r;
        obj %c;
        %r = A.room_at;
        if(%r) {
            if(%r.type_key == "security") {
                O = A.boss;
                if(O.is_at_work_club) {
                    $allow_it = true;
                    # this is based on how much the owner wants this done
                    $ai_prob = &prob_aff(O, "spying");
                }
            }
        }
    }
    allow $allow_it;
    aiprob $ai_prob;
{
    # find a worker
    var $best_val, $val, $l_id, $res, $last, $today, $diff;
    char T, L;
    cont @EMP[];
    obj %ej;

    O = A.boss;
    if(O) {
        $l_id = 0 + &get_flag(A,"found_applicant");
        if($l_id > 0) {
            L = &id_to_char($l_id);
            if(L) {
                if(L.boss == O) {
                    # still working in club
                    $res = &rat_out_applicant(A, O, L);
                    if($res) {
                        &use_up_tick(A, 5);
                        return;
                    }
                }
            }
        }

        %c = O.club;
        if(%c) {
            $best_val = 0;
            $today = &curr_date() / ^TICKS_PER_DAY;
            EMP = &club_employees(%c, "");
            foreach E in EMP {
                if(E.id != O.id) {
                    if(E.id != A.id) {
                        %ej = E.job;
                        if(%ej) {
                            if(%ej.duration > 30) {
                                # start with a value bewteen 100-200
                                $val = &random()*100 + 100;
                                # add 10-50 points for salary
                                $val = %ej.salary/2000 + $val;
                                if(%ej.is_performer) {
                                    # add 20 points if its a performer
                                    $val = $val + 20;
                                }
                                # subtract -20 to 20 points based on opinion
                                $val = 0 - &opinion_value(A, E) / 5 + $val;
                                # subtract points based on last check
                                $last = 0 + &get_flag(E, "last_spy_check");
                                if($last > 0) {
                                    $diff = $today - $last;
                                    if($diff < 120) {
                                        $val = $val - 120 + $diff;
                                    }
                                }
                                debug "Security: got val for " ^ E.name ^ " of " ^ $val;
                                if($val > $best_val) {
                                    $best_val = $val;
                                    T = E;
                                }
                            }
                        }
                    }
                }
            }

            if(T) {
                &spy_on_someone(A, O, T);
                &set_flag(T, "last_spy_check", $today, 120);
            }
        }
    }
    &use_up_tick(A, 10);
    return;
}

# A: bouncer, O: owner, T: target
function &spy_on_someone_check(A, O, T) {
    if(A.boss != O) {
        # A no longer O's employee
        return;
    }
    if(T.boss != O) {
        # T no longer O's employee
    }
    if(O.is_at_work_club) {
        &spy_on_someone(A, O, T);
        return;
    }
    sched 1 days, 30, &security::spy_on_someone_check(A, O, T);
    return;
}

# A: bouncer, O: owner, T: target
function &spy_on_someone(A, O, T) {
    var $ans, $like_key, $like_val, $like_str, $like_name, $learned, $emp_id;
    var $retry, $no_strip, $not_sexy;
    obj %j;

    debug "Spying " ^ A.name ^ " on " ^ T.name;
    $like_key = &util::get_random_like_key(T, "positive");
    if($like_key == "") {
        $retry = true;
    }
    elsif(&has_knowledge(O, T, $like_key)) {
        $retry = true;
    }
    else {
        $retry = false;
    }
    if($retry) {
        $like_key = &util::get_random_like_key(T, "notneut");
        if($like_key == "") {
            $retry = true;
        }
        elsif(&has_knowledge(O, T, $like_key)) {
            $retry = true;
        }
        else {
            $retry = false;
        }
        if($retry) {
            $like_key = &util::get_random_like_key(T, "");
        }
    }

    # special check for performers undressing
    $no_strip = false;
    $not_sexy = false;
    %j = T.job;
    if(%j) {
        if(%j.is_performer) {
            if(%j.job_key == "stripper") {
                if(! &emp_clothing::check_willing_to_undress(T)) {
                    $no_strip = true;
                }
            }
        }
    }
    if(! $no_strip) {
        # check for sexy clothing
        if(T.is_at_work) {
            if(! &emp_clothing::check_sexy_clothing(T, %j)) {
                $not_sexy = true;
            }
        }
    }

    if($like_key != "") {
        &join(A, O);
        $ans = decision O, A, -, T {
            title "security.spy.title"
            body "security.spy.body"
            choice "ok", 1, "security.spy.opt.ok"
        }

        $like_val = &like_value(T, $like_key);
        $like_str = &like_as_str($like_val, false);
        $like_name = &id_to_name($like_key, "like");
        $like_str = $like_str ^ " " ^ $like_name;
        $learned = &learn_knowledge(O, T, $like_key);
        $ans = decision O, A, -, T {
            title "security.spy.title"
            body "security.spy.info.body"~$like_str
            bodyif ! $learned, "security.spy.info.known.body"
            bodyif $no_strip, "security.spy.no_strip.body"
            bodyif $not_sexy, "security.spy.not_sexy.body"
            body "hdyr"
            choice "thank", 100, "security.spy.info.opt.thank"
            choiceif "nohelp", ! $learned, 1, "security.spy.info.opt.nohelp"
            choice "spy", 1, "security.spy.info.opt.spy"
        }

        if($ans == "thank") {
            &modify_relation(O, A, "good_job");
        }
        elsif($ans == "nohelp") {
        }
        elsif($ans == "spy") {
            $ans = decision O, A {
                title "security.spy.title"
                body "security.spy.list.body"
                choice "ok", 1, "security.spy.list.opt.ok"
            }
            $emp_id = select employees O, "weighted,job_skill";
            if($emp_id > 0) {
                T = &id_to_char($emp_id);
                if(T) {
                    if(T != A) {
                        $ans = decision O, A, -, T {
                            title "security.spy.title"
                            body "security.spy.list.gotit.body"
                            choice "ok", 1, "ok"
                        }
                        # 30 = 011110 = 1E (noon-aft-eve-night)
                        sched 6 days, 30, &security::spy_on_someone_check(A, O, T);
                        &set_flag(A, "_aie_&security::spy_on_workers", "X", 7);
                    }
                }
            }
        }
        &go_to_loc(A, "default");
    }
    return;
}

# called when E applies for another job to see if the security team at
# E's current club will find out about it
function &someone_applying_for_job(E, %to_club) {
    obj %curr_club;
    obj %sec;
    cont @BL[];
    var $prob,$r;
    char B;

    $prob = 0;              # prob of getting caught
    %curr_club = E.club;
    if(%curr_club) {
        if(%curr_club != %to_club) {
            %sec = &get_room(%curr_club, "security");
            if(%sec) {
                BL = &club_employees(%curr_club, "bouncer");
                foreach BO in BL {
                    if(BO.id != E.id) {
                        $prob = $prob + 35 + &get_attr(BO, "focus", 25);
                        if(B) {
                            if(&random() < 0.5) {
                                B = BO;
                            }
                        }
                        else {
                            B = BO;
                        }
                    }
                }
                $prob = $prob - &get_attr(E, "intel", 15) + &get_attr(E, "stealth", 10);
                $prob = 0 - &like_value(E, "attention") * 10 + $prob;
            }
        }
    }
    $r = &random() * 100 + &get_attr(B, "stealth", 10);
    if($r < $prob) {
        # got her
        debug "Bouncer " ^ B.name ^ " found out " ^ E.name ^ " is applying for another job";
        &set_flag(B, "found_applicant", E.id, 5);
        &update_attr(B, "stealth", 1);
    }
    return;
}

# Bouncer B rats out employee E to owner O for applying to another job
# return true or false depending if B did it
function &rat_out_applicant(B, O, E) {
    var $ans, $op_val, $emp_val;
    if(E.boss == O) {
        $op_val = &opinion_value(O, E);
        $ans = decision B, E {
            choice "rat", 60 - $op_val, "null"
            impact "rat", "will", 0.2
            impact "rat", "loyal", 0.3
            choice "hide", 40 + $op_val, "null"
            impact "hide", "comp", 0.1
        }
        if($ans == "rat") {
            &owner_lib::notify_owner(O, B.id, "security.emp_apply.msg", E.id, -1);
            if(O.is_ai) {
                # for simplicity, O will give a raise to E right here
                $emp_val = &owner_lib::emp_value(E, O);
                if(O.debt > 1000) {
                    $emp_val = 0 - O.debt/1000 + $emp_val;
                }
                $ans = decision O, E {
                    choice "raise", 100 + $emp_val , "null"
                    impact "raise", "intel", 0.5
                    choice "no", 100 - $emp_val, "null"
                    impact "no", "greed", 0.5
                }
                if($ans == "raise") {
                    &emp_mgmt::quick_raise(E, O, 0.05);
                }

            }
            return true;
        }
    }
    return false;
}

action &roam_about_club(A)
    details "Roam About Club >", 10, 5, -1, "club_emp at_work not_tired not_owner"
    init {
        var $ai_prob, $allow_it;
        obj %aj;
        %aj = A.job;
        if(%aj) {
            if(%aj.job_key == "bouncer") {
                $allow_it = true;
                $ai_prob = A.job_skill / 2 + 50;
            }
        }
    }
    allow $allow_it;
    aiprob $ai_prob;
{
    var $room_types;
    obj %go_to_r;
    # pick a room to go to
    $room_types = "seating,bar,vip,entrance";
    if(&random() > 0.5) {
        $room_types = $room_types ^ ",restrooms,game_room,pool,storage";
    }
    %go_to_r = &clubs::random_room(A.club, $room_types);
    if(%go_to_r) {
        &move_to_room(A, %go_to_r);
    }
    return;
}