package com.codewars.tenpinbowling;

import java.util.ArrayList;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class Solution {

    public static void main(String[] args) {
        System.out.println("Maybe this bowler should put bumpers on...\n ");
//        assertEquals(20, Solution.bowling_score("11 11 11 11 11 11 11 11 11 11"));
        System.out.println(Solution.bowling_score("11 11 11 11 11 11 11 11 11 11"));

        System.out.println("Woah! Perfect game!");
//        assertEquals(300, Solution.bowling_score("X X X X X X X X X XXX"));
        System.out.println(Solution.bowling_score("X X X X X X X X X XXX"));

        System.out.println("Should be 115");
        System.out.println(Solution.bowling_score("00 5/ 4/ 53 33 22 4/ 5/ 45 XXX"));

        System.out.println("Should be 150");
        System.out.println(Solution.bowling_score("5/ 4/ 3/ 2/ 1/ 0/ X 9/ 4/ 8/8"));

        System.out.println("should be 143");
        System.out.println(Solution.bowling_score("5/ 4/ 3/ 2/ 1/ 0/ X 9/ 4/ 7/2"));

        System.out.println("should be 171");
        System.out.println(Solution.bowling_score("X X 9/ 80 X X 90 8/ 7/ 44"));
    }

    public static int bowling_score(String frames) {
        System.out.println(frames);
        String[] rounds = frames.split(" ");
        ArrayList<Integer> roundsScore = new ArrayList<>();

        for (int i = 0; i < rounds.length - 1; i++) {
            char[] frame = rounds[i].toCharArray();
            for (char roll : frame) {
                switch (roll) {
                    case 'X':
                        roundsScore.add(10);
                        break;
                    case '/':
                        roundsScore.add(roundsScore.get(roundsScore.size() - 1) - 10);
                        break;
                    default:
                        roundsScore.add(Character.digit(roll, 10));
                }
            }
        }

        char[] lastFrame = rounds[rounds.length - 1].toCharArray();
        int lastFrameRolls = lastFrame.length;
        int lastRoll = 0;
        for (char roll : lastFrame) {
            int rollScore = 0;
            switch (roll) {
                case 'X':
                    rollScore += 10;
                    lastRoll = 0;
                    break;
                case '/':
                    rollScore += lastRoll - 10;
                    lastRoll = 0;
                    break;
                default:
                    rollScore += Character.digit(roll, 10);
                    lastRoll = Character.digit(roll, 10);
            }
            roundsScore.add(rollScore);
        }

        int score = 0;

        for (int i = 0; i < roundsScore.size() - lastFrameRolls; i++) {
            int thisRoll = roundsScore.get(i);
            if (thisRoll == 10) {
                int firstRollAfter = roundsScore.get(i + 1);
                thisRoll += firstRollAfter;
                int secondRollAfter = roundsScore.get(i + 2);
                secondRollAfter = secondRollAfter >= 0 ? secondRollAfter : calcSpare(firstRollAfter, 0);
                thisRoll += secondRollAfter;
            } else if (thisRoll < 0) {
                thisRoll = calcSpare(roundsScore.get(i - 1), roundsScore.get(i + 1));
            }
            score += thisRoll;
        }

        for (int i = roundsScore.size() - lastFrameRolls; i < roundsScore.size(); i++) {
            int thisRoll = roundsScore.get(i);
            if (thisRoll < 0) {
                thisRoll = calcSpare(roundsScore.get(i - 1), 0);
            }
            score += thisRoll;
        }
        return score;
    }

    private static int calcSpare(int before, int after) {
        return (10 - before) + after;
    }
}
