"use strict";
this.name = "hoopy_casino";
this.author = "Paul Wilkins (updated by Eric Walch, further updated by spara)";
this.copyright = "(C) 2009 Paul Wilkins";
this.license = "CC BY-NC-SA 3.0";

//add interface
this.startUpComplete = this.shipExitedWitchspace = function () {
    var casinos = system.shipsWithRole("casinoship");
    if (casinos.length > 0) {
        for (var i = 0; i < casinos.length; i++) {
            casinos[i].setInterface("hoopy-hoops", {
                title: expandMissionText("hoopy-hoops-title"),
                category: expandMissionText("hoopy-gambling-category"),
                summary: expandMissionText("hoopy-hoops-summary"),
                callback: this.$startGambling.bind(this)
            });
        }
    }
}

//init the hoops
this.shipDockedWithStation = function (station) {
    if (player.ship.docked && station.hasRole("casinoship")) {
        missionVariables.hoopy_casino = "NOT_NOW";
        this.setBoundaries();
    }
};

this.missionScreenOpportunity = function () {
    if (player.ship.docked && player.ship.dockedStation.hasRole("casinoship")) {
        this.gamble();
    }
};


this.setBoundaries = function () {
    /*
    In the legacy version the random distribution was bad. One could win after analysing the odds
    during some time. This is a feature that should stay in. So we prepare the gamble table now a bit.
    */
    this.boundary1 = 1 + Math.random() - Math.random();
    this.boundary2 = 2 + Math.random() - Math.random();
    if (this.boundary1 > this.boundary2) this.boundary1 = this.boundary2;
    this.startCapital = player.credits;
};

this.$startGambling = function () {
    missionVariables.hoopy_casino = "HOOPY_ENTER";
    this.gambleChoices("YES_HOOPY");
}

this.gamble = function () {
    /*
    if (missionVariables.hoopy_casino === "HOOPY_REVISIT") {
        if (player.credits >= 100) {
            mission.runScreen({titleKey: "hoopy_casino_title", messageKey: "hoopy_casino_intro", overlay: "hoopy_front.png", choicesKey: "hoopy_casino_enter_yesno", music: "hoopy_theme.ogg"}, this.gambleChoices);
            missionVariables.hoopy_casino = "HOOPY_ENTER";
            return;
        }
    }*/
    if (missionVariables.hoopy_casino === "HOOPY_GAMEON") {
        mission.runScreen({
            screenID: "hoopy_casino",
            titleKey: "hoopy_casino_title",
            messageKey: "hoopy_casino_choices",
            overlay: "hoopy_cover.png",
            choicesKey: "hoopy_casino_pick_hoop",
            music: "hoopy_theme.ogg"
        },
            this.gambleChoices);
        this.hoopy_casino_hoop = Math.random() * 3;
        missionVariables.hoopy_casino = "HOOPY_MAKE_CHOICE";
        return;
    }
    if (player.credits > this.startCapital + 2000) {
        mission.runScreen({
            screenID: "hoopy_casino",
            titleKey: "hoopy_casino_title",
            messageKey: "hoopy_casino_new_table",
            overlay: "hoopy_front.png",
            choicesKey: "hoopy_casino_new_table_yesno",
            music: "hoopy_theme.ogg"
        },
            this.gambleChoices);
        missionVariables.hoopy_casino = "HOOPY_NEW_TABLE";
        this.setBoundaries();
    } else {
        if (missionVariables.hoopy_casino === "HOOPY_WINNER") {
            mission.runScreen({
                screenID: "hoopy_casino",
                titleKey: "hoopy_casino_title",
                messageKey: "hoopy_casino_win",
                overlay: this.hoopy_casino_image,
                choicesKey: "hoopy_casino_replay_winner_yesno",
                music: "hoopy_theme.ogg"
            },
                this.gambleChoices);
            player.credits += 100;
            missionVariables.hoopy_casino = "HOOPY_REPLAY";
        }
        if (missionVariables.hoopy_casino === "HOOPY_LOSER") {
            mission.runScreen({
                screenID: "hoopy_casino",
                titleKey: "hoopy_casino_title",
                messageKey: "hoopy_casino_loss",
                overlay: this.hoopy_casino_image,
                choicesKey: "hoopy_casino_replay_loser_yesno",
                music: "hoopy_theme.ogg"
            },
                this.gambleChoices);
            player.credits -= 100;
            missionVariables.hoopy_casino = "HOOPY_REPLAY";
        }
    }
    if (missionVariables.hoopy_casino === "NOT_NOW") {
        delete this.hoopy_casino_hoop;
        delete this.hoopy_casino_image;
        delete missionVariables.hoopy_casino;
    }
};
this.gambleChoices = function (choice) {
    if (missionVariables.hoopy_casino === "HOOPY_ENTER") {
        if (choice === "YES_HOOPY") {
            if (player.credits >= 100) {
                mission.runScreen({
                    screenID: "hoopy_casino",
                    titleKey: "hoopy_casino_title",
                    messageKey: "hoopy_casino_desc",
                    overlay: "hoopy_clean.png",
                    choicesKey: "hoopy_casino_gamble_yesno",
                    music: "hoopy_theme.ogg"
                },
                    this.gambleChoices);
                missionVariables.hoopy_casino = "HOOPY_GAMBLE";
            }
            else {
                missionVariables.hoopy_casino = "HOOPY_REPLAY";
                this.gambleChoices("YES_HOOPY");
                return;
            }
        } else if (choice === "NO_HOOPY") {
            missionVariables.hoopy_casino = "NOT_NOW";
        }
        return;
    }
    if (missionVariables.hoopy_casino === "HOOPY_GAMBLE") {
        if (choice === "0_HOOPY") {
            mission.runScreen({
                screenID: "hoopy_casino",
                titleKey: "hoopy_casino_title",
                messageKey: "hoopy_casino_desc",
                overlay: "hoopy_clean.png",
                choicesKey: "hoopy_casino_gamble_yesno",
                music: "hoopy_theme.ogg"
            },
                this.gambleChoices);
        } else if (choice === "YES_HOOPY") {
            missionVariables.hoopy_casino = "HOOPY_GAMEON";
        } else if (choice === "NO_HOOPY") {
            missionVariables.hoopy_casino = "NOT_NOW";
        }
        return;
    }
    if (missionVariables.hoopy_casino === "HOOPY_MAKE_CHOICE") {
        missionVariables.hoopy_casino = "HOOPY_LOSER";
        if (this.hoopy_casino_hoop < this.boundary1) {
            this.hoopy_casino_image = "hoopy_Lwin.png";
            if (choice === "LEFT_HOOPY") {
                missionVariables.hoopy_casino = "HOOPY_WINNER";
            }
        }
        if (this.hoopy_casino_hoop >= this.boundary1 && this.hoopy_casino_hoop < this.boundary2) {
            this.hoopy_casino_image = "hoopy_Cwin.png";
            if (choice === "MIDDLE_HOOPY") {
                missionVariables.hoopy_casino = "HOOPY_WINNER";
            }
        }
        if (this.hoopy_casino_hoop >= this.boundary2) {
            this.hoopy_casino_image = "hoopy_Rwin.png";
            if (choice === "RIGHT_HOOPY") {
                missionVariables.hoopy_casino = "HOOPY_WINNER";
            }
        }
        return;
    }
    if (missionVariables.hoopy_casino === "HOOPY_NEW_TABLE") {
        if (choice === "NEW_TABLE_HOOPY") {
            mission.runScreen({
                screenID: "hoopy_casino",
                titleKey: "hoopy_casino_title",
                messageKey: "hoopy_casino_desc",
                overlay: "hoopy_clean.png",
                choicesKey: "hoopy_casino_gamble_yesno",
                music: "hoopy_theme.ogg"
            },
                this.gambleChoices);
            missionVariables.hoopy_casino = "HOOPY_GAMBLE";
        } else if (choice === "NO_HOOPY") {
            missionVariables.hoopy_casino = "NOT_NOW";
        }
        return;
    }
    if (missionVariables.hoopy_casino === "HOOPY_REPLAY") {
        if (choice === "0-WINNER_HOOPY") {
            mission.runScreen({
                screenID: "hoopy_casino",
                titleKey: "hoopy_casino_title",
                messageKey: "hoopy_casino_win",
                overlay: this.hoopy_casino_image,
                choicesKey: "hoopy_casino_replay_winner_yesno",
                music: "hoopy_theme.ogg"
            },
                this.gambleChoices);
        }
        if (choice === "0-LOSER_HOOPY") {
            mission.runScreen({
                screenID: "hoopy_casino",
                titleKey: "hoopy_casino_title",
                messageKey: "hoopy_casino_loss",
                overlay: this.hoopy_casino_image,
                choicesKey: "hoopy_casino_replay_loser_yesno",
                music: "hoopy_theme.ogg"
            },
                this.gambleChoices);
        }
        if (choice === "YES_HOOPY") {
            if (player.credits >= 100) {
                mission.runScreen({
                    screenID: "hoopy_casino",
                    titleKey: "hoopy_casino_title",
                    messageKey: "hoopy_casino_again",
                    overlay: "hoopy_clean.png",
                    music: "hoopy_theme.ogg"
                });
                missionVariables.hoopy_casino = "HOOPY_GAMEON";
            } else {
                mission.runScreen({
                    screenID: "hoopy_casino",
                    titleKey: "hoopy_casino_title",
                    messageKey: "hoopy_casino_nofunds",
                    overlay: "hoopy_front.png",
                    music: "hoopy_theme.ogg"
                });
                missionVariables.hoopy_casino = "NOT_NOW";
            }
        }
        if (choice === "NO_HOOPY") {
            mission.runScreen({
                screenID: "hoopy_casino",
                titleKey: "hoopy_casino_title",
                messageKey: "hoopy_casino_goodbye",
                overlay: "hoopy_front.png",
                music: "hoopy_theme.ogg"
            });
            missionVariables.hoopy_casino = "NOT_NOW";
        }
    }
};
