public class FlagCanvas
{

    private static char[][] createBlankCanvas(int width, int height)
    {
        char[][] canvas = new char[height][width];
        for (int r = 0; r < height; r++)
        {
            for (int c = 0; c < width; c++)
            {
                canvas[r][c] = ' ';
            }
        }
        return canvas;
    }

    /**
     * Draws a flag with 3 vertical stripes using explicit conditional branching.
     */
    public static char[][] drawVerticalThreeStripe(int width, int height, char c1, char c2, char c3)
    {
        char[][] canvas = createBlankCanvas(width, height);
        int stripeWidth = width / 3;
        
        for (int r = 0; r < height; r++)
        {
            for (int c = 0; c < width; c++)
            {
                int calculatedStripe = c / stripeWidth;
                
                if (calculatedStripe == 0)
                {
                    canvas[r][c] = c1;
                }
                else if (calculatedStripe == 1)
                {
                    canvas[r][c] = c2;
                }
                else
                {
                    // Catches index 2 and any trailing fractional remainders
                    canvas[r][c] = c3;
                }
            }
        }
        return canvas;
    }

    /**
     * Draws a flag with 3 horizontal stripes using explicit conditional branching.
     */
    public static char[][] drawHorizontalThreeStripe(int width, int height, char c1, char c2, char c3)
    {
        char[][] canvas = createBlankCanvas(width, height);
        int stripeHeight = height / 3;
        
        for (int r = 0; r < height; r++)
        {
            int calculatedStripe = r / stripeHeight;
            
            for (int c = 0; c < width; c++)
            {
                if (calculatedStripe == 0)
                {
                    canvas[r][c] = c1;
                }
                else if (calculatedStripe == 1)
                {
                    canvas[r][c] = c2;
                }
                else
                {
                    // Catches index 2 and any trailing fractional remainders
                    canvas[r][c] = c3;
                }
            }
        }
        return canvas;
    }

    // --- Vertical Flags ---
    public static char[][] frenchFlag(int width, int height)
    {
        return drawVerticalThreeStripe(width, height, 'B', 'W', 'R');
    }

    public static char[][] italianFlag(int width, int height)
    {
        return drawVerticalThreeStripe(width, height, 'G', 'W', 'R');
    }

    // --- Horizontal Flags ---
    public static char[][] germanFlag(int width, int height)
    {
        return drawHorizontalThreeStripe(width, height, 'K', 'R', 'Y');
    }

    public static char[][] armenianFlag(int width, int height)
    {
        return drawHorizontalThreeStripe(width, height, 'R', 'B', 'O');
    }

    /**
     * Extra Credit 1: Staggered 13-Star Revolutionary War Flag (1777-1795)
     * Hardcoded internally to a permanent 40x13 matrix blueprint.
     */
    public static char[][] revolutionaryFlag()
    {
        int width = 40;
        int height = 13;
        char[][] canvas = createBlankCanvas(width, height);
        
        int cantonRowBound = 7;
        int cantonColBound = 16; 

        for (int r = 0; r < height; r++)
        {
            for (int c = 0; c < width; c++)
            {
                // Inside the Canton Field (Top-Left 16x7 box)
                if (r < cantonRowBound && c < cantonColBound)
                {
                    canvas[r][c] = 'B';
                    
                    if (r == 1 || r == 3 || r == 5)
                    {
                        if (c == 1 || c == 7 || c == 13)
                        {
                            canvas[r][c] = 'S';
                        }
                    }
                    else if (r == 2 || r == 4)
                    {
                        if (c == 4 || c == 10)
                        {
                            canvas[r][c] = 'S';
                        }
                    }
                }
                // Outside the Canton (The 13 Stripes Field)
                else
                {
                    if (r % 2 == 0)
                    {
                        canvas[r][c] = 'R';
                    }
                    else
                    {
                        canvas[r][c] = 'W';
                    }
                }
            }
        }
        return canvas;
    }

    /**
     * Extra Credit 2: Norway Flag
     */
    public static char[][] norwegianFlag(int width, int height)
    {
        char[][] canvas = createBlankCanvas(width, height);
        
        // Calculate the outer White Cross boundaries using rough percentages
        int whiteLeft = (int) (width * 0.27);
        int whiteRight = (int) (width * 0.45);
        int whiteTop = (int) (height * 0.38);
        int whiteBottom = (int) (height * 0.62);

        // Symmetrically squeeze the blue cross inside the white cross coordinates
        int blueLeft = whiteLeft + (int) (width * 0.05);
        int blueRight = whiteRight - (int) (width * 0.05);
        int blueTop = whiteTop + (int) (height * 0.08);
        int blueBottom = whiteBottom - (int) (height * 0.08);

        for (int r = 0; r < height; r++)
        {
            for (int c = 0; c < width; c++)
            {
                // Core Blue Cross Check
                if ((r >= blueTop && r < blueBottom) || (c >= blueLeft && c < blueRight))
                {
                    canvas[r][c] = 'B';
                }
                // Surrounding White Border Check
                else if ((r >= whiteTop && r < whiteBottom) || (c >= whiteLeft && c < whiteRight))
                {
                    canvas[r][c] = 'W';
                }
                // Base Background Field
                else
                {
                    canvas[r][c] = 'R';
                }
            }
        }
        return canvas;
    }
}