<script type="text/javascript">
    var crapsComponent = new Vue({
        el: '#crapsComponent',
        delimiters: ['@{{', '}}'],
        data: {
            game: false,
            chips: [1, 5, 10, 25, 50, 100],
            total: 10000,
            max: 2000,
            selectedChip: 1,
            alert: 'Place your bets by clicking on the Pass Line',
            comeAlert: '',
            passChipCount: 0,
            passchips: [],
            passPoint: 0,
            comeChipCount: 0,
            currentComeChips: [],
            comePoints: [],
            oddsChipCount: 0,
            oddschips: [],
            oddsAlert: '',
            rolledOnce: false,
            rolled: false,
            rollCount: 0,
            dice1Text: 'fas fa-dice-two',
            dice2Text: 'fas fa-dice-three',
        },
        methods: {
            currencyFormat: function(_num) {
                var formatter = new Intl.NumberFormat('en-US', {
                    style: 'currency',
                    currency: 'USD',
                });
                return formatter.format(_num);
            },
            onPassLine: function() {
                var self = this;
                if (self.passPoint) return;
                if (self.passPoint >= 2000) return;
                self.game = true;
                self.comeAlert = '';
                self.alert = 'Place your bets by clicking on the Pass Line';
                self.passChipCount += self.selectedChip;
                if (self.passchips.indexOf(self.selectedChip) <= -1) {
                    self.passchips.push(self.selectedChip);
                };
                self.total -= self.selectedChip;
            },
            onComeLine: function() {
                var self = this;
                if (!self.passPoint) return;
                self.game = true;
                self.comeAlert = 'Place new bets by clicking on the COME';
                self.comeChipCount += self.selectedChip;
                if (self.currentComeChips.indexOf(self.selectedChip) <= -1) {
                    self.currentComeChips.push(self.selectedChip);
                };
                self.total -= self.selectedChip;
            },
            onOddsLine: function() {
                var self = this;
                if (!self.passPoint) return;
                self.oddsAlert = "";
                var max = self.passChipCount * 3;
                if (self.selectedChip > max || (self.oddsChipCount + self.selectedChip) > max) {
                    self.total += self.oddsChipCount;
                    self.oddschips = self.passchips;
                    self.oddsChipCount = max;
                    self.oddsAlert = "Max odds bet reached! (x3 of your pass bet)";
                    self.total -= max;
                } else {
                    self.oddsChipCount += self.selectedChip;
                    if (self.oddschips.indexOf(self.selectedChip) <= -1) {
                        self.oddschips.push(self.selectedChip);
                    };
                    self.total -= self.selectedChip;
                }
            },
            resetGame: function() {
                var self = this;
                self.passchips = [];
                self.passChipCount = 0;
                self.passPoint = 0;
                self.oddschips = [];
                self.oddsChipCount = 0;
                self.rolled = false;
                setTimeout(function() {
                    if (self.comePoints.length == 0) {
                        self.game = false;
                    };
                }, 1)
            },
            resetPassGame: function() {
              var self = this;
              self.total += self.passChipCount;
              self.passChipCount = 0;
              self.passchips = [];
              setTimeout(function() {
                  if (self.comePoints.length == 0) {
                      self.game = false;
                  };
              }, 1)
            },
            resetComeGame: function() {
                var self = this;
                self.currentComeChips = [];
                self.comeChipCount = 0;
                self.currentComeChips = [];
                self.comePoints = [];
            },
            resetOddsGame: function () {
              var self = this;
              self.total += self.oddsChipCount;
              self.oddsChipCount = 0;
              self.oddschips = [];
            },
            onRollDice: function() {
                var self = this;
                self.rolledOnce = true;
                self.rolled = true;
                self.dice1 = Math.floor(Math.random() * 6) + 1;
                self.dice2 = Math.floor(Math.random() * 6) + 1;

                self.dice1Text = 'fas fa-dice-' + self.numtoString(self.dice1);
                self.dice2Text = 'fas fa-dice-' + self.numtoString(self.dice2);
                if (!self.passPoint || self.comePoints.length > 1) {
                    self.passGame();
                } else {
                    self.mainGame();
                }
            },
            numtoString: function(_num) {
                switch (_num) {
                    case 1:
                        return 'one';
                        break;
                    case 2:
                        return 'two';
                        break;
                    case 3:
                        return 'three';
                        break;
                    case 4:
                        return 'four';
                        break;
                    case 5:
                        return 'five';
                        break;
                    case 6:
                        return 'six';
                        break;
                }
            },
            passGame: function() {
                var self = this;
                if (self.comePoints.length > 0) {
                    self.mainGame();
                };
                self.oddsAlert = '';
                if (self.passChipCount == 0) return;
                var totalDice = self.dice1 + self.dice2;
                if (totalDice == 7 || totalDice == 11) {
                    self.alert = 'Game Won! You rolled ' + totalDice;
                    self.total += (self.passChipCount * 2);
                    self.resetGame();
                } else if (totalDice == 2 || totalDice == 3 || totalDice == 12) {
                    self.alert = 'Game Lost! You rolled ' + totalDice;
                    self.resetGame();
                } else {
                    self.passPoint = totalDice;
                    self.alert = "Roll the dice until you get " + self.passPoint + " to win";
                }
            },
            mainGame: function() {
                var self = this;
                var totalDice = self.dice1 + self.dice2;
                if (self.passChipCount > 0) { //ensure pass game is ongoing
                    if (totalDice == self.passPoint) {
                        self.alert = 'Game Won! You rolled ' + totalDice;
                        self.total += (self.passChipCount * 2);
                        if (self.oddsChipCount > 0) {
                            self.oddsGame();
                        }else {
                          self.resetGame();
                        }
                    } else if (totalDice == 7) {
                        self.alert = 'Game Lost! You rolled ' + totalDice;
                        if (self.oddsChipCount > 0) {
                            self.oddsGame();
                        }else {
                          self.resetGame();
                        }
                    };
                };

                if (self.comePoints.length > 0) {
                    self.comeAlert = '';
                    var multipleAlerts = [];
                    for (var i = 0; i < self.comePoints.length; i++) {
                        if (totalDice == self.comePoints[i].move) {
                            multipleAlerts.push('Come Game Won! You rolled ' + totalDice);
                            self.total += (self.comePoints[i].chipCount * 2);
                            self.comePoints.splice(i, 1);
                        } else if (totalDice == 7) {
                            multipleAlerts.push('Come Game Lost! You rolled ' + totalDice);
                            self.comePoints.splice(i, 1);
                        } else {
                            multipleAlerts.push("Roll the dice until you also get " + self.comePoints[i].move + " to win");
                        };
                    };
                    self.comeAlert = multipleAlerts.toString();
                };
                if (self.comePoints.length == 0 && !self.passPoint) {
                    self.resetGame();
                }
                if (self.comeChipCount > 0) {
                    self.comeGame(totalDice);
                }
            },
            comeGame: function(_totalDice) {
                var self = this;
                if (self.currentComeChips.length > 0) {
                    if (_totalDice == 7 || _totalDice == 11) {
                        self.comeAlert = 'Come Game Won! You rolled ' + _totalDice + '\n';
                        self.total += (self.currentComeChips * 2);
                        if (self.comePoints.length == 0) {
                            self.resetComeGame();
                        }
                    } else if (_totalDice == 2 || _totalDice == 3 || _totalDice == 12) {
                        self.comeAlert = 'Come Game Lost! You rolled ' + _totalDice + '\n';
                        if (self.comePoints.length == 0) {
                            self.resetComeGame();
                        }
                    } else {
                        var obj = {
                            move: _totalDice,
                            chipCount: self.comeChipCount
                        };
                        self.comePoints.push(obj);
                        self.comeAlert = "Roll the dice until you also get " + _totalDice + " to win";
                        self.comeChipCount = 0;
                    };
                }
            },
            oddsGame: function() {
                var self = this;
                if (self.oddsChipCount == 0) return;
                var totalDice = self.dice1 + self.dice2;
                if (totalDice == self.passPoint) {
                    self.oddsAlert = 'Odds Game Won! You rolled ' + totalDice;
                    self.total += (self.oddsChipCount * 2);
                    self.resetGame();
                } else if (totalDice == 7) {
                    self.oddsAlert = 'Odds Game Lost! You rolled ' + totalDice;
                    self.resetGame();
                } else {
                    self.oddsAlert = "Win you pass point to win your odds!";
                }
            },
            totalCounter: function() {
                var self = this;
            }
        },
        mounted: function() {

        }
    });
</script>
