package me.hammerle.supersnuvi.gamelogic;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import me.hammerle.supersnuvi.javafx.KeyHandler;
import me.hammerle.supersnuvi.savegame.SimpleConfig;
import me.hammerle.supersnuvi.tiles.*;
import me.hammerle.supersnuvi.util.SoundUtils;
import me.hammerle.supersnuvi.rendering.IRenderer;

public class StateRenderer
{
    // constants
    public static final ColoredBaseTile FALLBACK_TILE = new ColoredBaseTile(0, 0, 0, 0);
    
    // rendering
    private final IRenderer renderer;   
    
    // tiles
    private final HashMap<Integer, Tile> registeredTiles;
    
    // levels
    private Level currentLevel;
    private final Level[] levels;
    private int levelIndex;
    public static final SimpleConfig CONFIG = new SimpleConfig("savegame.txt");

    public StateRenderer(IRenderer renderer)
    {
        CONFIG.load();
        
        this.renderer = renderer;
        this.registeredTiles = new HashMap<>();
        
        File[] files = new File("./levels").listFiles();
        Arrays.sort(files, (o1, o2) -> o1.compareTo(o2));
        levels = new Level[files.length];
        for(int i = 0; i < levels.length; i++)
        {
            levels[i] = new Level(this, files[i]);
        }
        currentLevel = null;
        levelIndex = 0;

        SoundUtils.initSounds();
        
        registerTiles();
    }    

    // -------------------------------------------------------------------------
    // tile stuff
    // -------------------------------------------------------------------------
    
    private void registerTiles()
    {
        // dirt
        for(int i = 0; i < 16; i++)
        {
            registeredTiles.put(i, new BaseBoxTile("dirt/dirt" + i));
        }
        
        // grass
        registeredTiles.put(20, new BaseBoxTile("grass/grass4"));
        registeredTiles.put(21, new BaseBoxTile("grass/grass9"));
        registeredTiles.put(22, new BaseBoxTile("grass/grass8"));
        registeredTiles.put(23, new BaseBoxTile("grass/grass10"));
        registeredTiles.put(28, new BaseBoxTile("grass/grass7"));
        registeredTiles.put(29, new BaseBoxTile("grass/grass15"));
        registeredTiles.put(30, new BaseBoxTile("grass/grass16"));
        registeredTiles.put(31, new BaseBoxTile("grass/grass14"));
        
        // bottled soul
        registeredTiles.put(BottledSoulTile.ID, new BottledSoulTile());
        
        // bounce shroom
        registeredTiles.put(48, new TrampolinTile());
        
        // crumbling stones
        registeredTiles.put(64, new CrumblingStoneTile());
        
        // spike trap
        registeredTiles.put(80, new SpikeTile());
        
        // water
        registeredTiles.put(96, new WaterTile(true));
        registeredTiles.put(97, new WaterTile(false));
        
        // snuvi start block
        registeredTiles.put(StartTile.ID, new StartTile());
        
        // sky
        registeredTiles.put(128, new SkyTile());
        
        // ice
        registeredTiles.put(144, new SlipperyTile("ice/ice"));
        
        // slippery slime
        registeredTiles.put(148, new SlipperyTile("slippery_slime/slippery_slime148"));
        registeredTiles.put(149, new SlipperyTile("slippery_slime/slippery_slime149"));
        registeredTiles.put(150, new SlipperyTile("slippery_slime/slippery_slime150"));
        registeredTiles.put(151, new SlipperyTile("slippery_slime/slippery_slime151"));
        registeredTiles.put(156, new SlipperyTile("slippery_slime/slippery_slime156"));
        registeredTiles.put(157, new SlipperyTile("slippery_slime/slippery_slime157"));
        registeredTiles.put(158, new SlipperyTile("slippery_slime/slippery_slime158"));
        registeredTiles.put(159, new SlipperyTile("slippery_slime/slippery_slime159"));
        
        // end level
        registeredTiles.put(160, new GoalTile("end/end"));
        registeredTiles.put(161, new GoalTile("end/end2"));
        
        // thorns
        registeredTiles.put(176, new KillTile("thorns/thorns_bottom", 2));
        registeredTiles.put(177, new KillTile("thorns/thorns_mid", 2));
        registeredTiles.put(178, new KillTile("thorns/thorns_top", 2));
        registeredTiles.put(179, new KillTile("thorns/thorns_bottom", -3));
        registeredTiles.put(180, new KillTile("thorns/thorns_mid", -3));
        registeredTiles.put(181, new KillTile("thorns/thorns_top", -3));
        
        // decoration shrooms
        registeredTiles.put(208, new DecoShroomTile("shrooms/shroom"));
        registeredTiles.put(209, new DecoShroomTile("shrooms/shroom2"));
        registeredTiles.put(210, new DecoShroomTile("shrooms/shroom3"));
        registeredTiles.put(211, new DecoShroomTile("shrooms/shroom4"));
    }
    
    public Tile getTile(int id)
    {
        Tile tile = registeredTiles.get(id);
        if(tile == null)
        {
            return FALLBACK_TILE;
        }
        return tile;
    }
    
    public Tile getInteractionTile(int id)
    {
        return registeredTiles.getOrDefault(id, FALLBACK_TILE);
    }
    
    public void resetTiles()
    {
        registeredTiles.values().forEach(v -> v.reset());
    }
    
    // -------------------------------------------------------------------------
    // tick, rendering
    // -------------------------------------------------------------------------

    public IRenderer getRenderer() 
    {
        return renderer;
    }
    
    private static final int MENU_WIDTH = 26;
    private static final int MENU_MAX = 5;
    private static final char[] TABLE_TOP = getCharLine((char) 131, (char) 136, (char) 133, (char) 136);
    private static final char[] TABLE_HEADING = getCharLine((char) 134, "Wähle ein Level ...", (char) 134, ' ');
    private static final char[] TABLE_MID = getCharLine((char) 130, (char) 136, (char) 132, (char) 129);
    private static final char[] TABLE_BOTTOM = getCharLine((char) 137, (char) 136, (char) 138, (char) 135);
    private static final char[] TABLE_MORE = getCharLine((char) 134, "...", (char) 134, (char) 134);
            
    private static char[] getCharLine(char start, char mid, char end, char spacer)
    {
        char[] c = new char[MENU_WIDTH];
        Arrays.fill(c, 1, c.length - 1, mid);
        c[0] = start;
        c[c.length - 1] = end;
        c[c.length - 3] = spacer;
        return c;
    }
    
    private static char[] getCharLine(char start, String s, char end, char spacer)
    {
        char[] chars = new char[MENU_WIDTH];
        chars[0] = start;
        chars[chars.length - 1] = end;
        int border = Math.min(MENU_WIDTH - 2, s.length());
        for(int i = 0; i < border; i++)
        {
            chars[i + 1] = s.charAt(i);
        }
        chars[chars.length - 3] = spacer;
        return chars;
    }
    
    public void update()
    {
        if(currentLevel != null)
        {
            SoundUtils.playSound(SoundUtils.Sound.SONG_1);
            SoundUtils.stopSound(SoundUtils.Sound.MENU_MUSIC);
            
            currentLevel.update();
            
            // doing that here to prevent concurent modification
            if(currentLevel.shouldFinish())
            {
                currentLevel.resetLevel();
                CONFIG.set("level." + currentLevel.getName(), true);
                currentLevel = null;
                CONFIG.save();
                return;
            }
            
            if(currentLevel.shouldReset())
            {
                currentLevel.resetLevel();
            }
            
            if(KeyHandler.ESCAPE.wasJustReleased())
            {
                currentLevel = null;
            }
        }
        else
        {
            SoundUtils.playSound(SoundUtils.Sound.MENU_MUSIC);
            SoundUtils.stopSound(SoundUtils.Sound.SONG_1);
            
            if(KeyHandler.ENTER.wasJustReleased())
            {
                currentLevel = levels[levelIndex];
                return;
            }

            if(KeyHandler.DOWN.getDownTime() > 16)
            {
                KeyHandler.DOWN.resetTime();
                levelIndex++;
            }
            else if(KeyHandler.UP.getDownTime() > 16)
            {
                KeyHandler.UP.resetTime();
                levelIndex--;
            }
            else if(KeyHandler.DOWN.wasJustReleased())
            {
                levelIndex++;
            }
            else if(KeyHandler.UP.wasJustReleased())
            {
                levelIndex--;
            }
            if(levelIndex < 0)
            {
                levelIndex = 0;
            }
            else if(levelIndex >= levels.length)
            {
                levelIndex = levels.length - 1;
            }
        }
        
        SoundUtils.playSounds();
    }
    
    public void render()
    {
        if(currentLevel != null)
        {
            currentLevel.render();
            return;
        }
        
        // level screen rendering
        renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 3 / 2);
        
        int listLength = Math.min(levels.length, MENU_MAX);
        double x = (renderer.getWidth() - renderer.getTextWidth(MENU_WIDTH)) * 0.5;
        double y = (renderer.getHeight() - renderer.getTextHeight(listLength + 4)) * 0.5;
        double line = renderer.getTextHeight(1);
        
        renderer.drawText(x, y, TABLE_TOP);
        y += line;
        renderer.drawText(x, y, TABLE_HEADING);
        y += line;
        renderer.drawText(x, y, TABLE_MID);
        y += line;

        if(levels.length > MENU_MAX)
        {
            int upperHalf = MENU_MAX / 2;
            int downHalf = levels.length - upperHalf;
            if(levelIndex < upperHalf)
            {
                paintStringMarking(x, y, line, levelIndex);
                y = paintLevelName(x, y, line, 0, MENU_MAX - 1);
                renderer.drawText(x, y, TABLE_MORE);
                y += line;
            }
            else if(levelIndex >= downHalf)
            {
                paintStringMarking(x, y, line, levelIndex - downHalf + upperHalf + ((MENU_MAX & 1) != 0 ? 1 : 0));
                renderer.drawText(x, y, TABLE_MORE);
                y += line;
                y = paintLevelName(x, y, line, levels.length - MENU_MAX + 1, MENU_MAX - 1);
            }
            else
            {
                paintStringMarking(x, y, line, upperHalf);
                renderer.drawText(x, y, TABLE_MORE);
                y += line;
                y = paintLevelName(x, y, line, levelIndex - upperHalf + 1, MENU_MAX - 2);
                renderer.drawText(x, y, TABLE_MORE);
                y += line;
            }
        }
        else
        {
            paintStringMarking(x, y, line, levelIndex);
            y = paintLevelName(x, y, line, 0, levels.length);
        }

        renderer.drawText(x, y, TABLE_BOTTOM);
        renderer.stopTextDrawing();
    }
    
    private double paintLevelName(double x, double y, double line, int from, int length)
    {
        char[] chars = new char[MENU_WIDTH];
        chars[0] = 134;
        chars[MENU_WIDTH - 1] = 134;

        length += from;
        for(int j = from; j < length; j++)
        {
            String s = levels[j].getName();
            int border = Math.min(MENU_WIDTH - 4, s.length());
            for(int i = 0; i < border; i++)
            {
                chars[i + 1] = s.charAt(i);
            }
            Arrays.fill(chars, border + 1, MENU_WIDTH - 1, (char) 0);
            chars[chars.length - 3] = 134;
            if(CONFIG.getBoolean("level." + s, false))
            {
                chars[chars.length - 2] = 'x';
            }
            else
            {
                chars[chars.length - 2] = 'o';
            }
            renderer.drawText(x, y, chars);
            y += line;
        }
        return y;
    }
    
    private void paintStringMarking(double x, double y, double line, int pos)
    {
        renderer.save();
        renderer.setFillColor(128, 128, 128, 1.0);
        renderer.fillRectangle(
                x + renderer.getTextWidth(1),
                y + renderer.getTextHeight(pos) - 1, 
                renderer.getTextWidth(MENU_WIDTH - 4), 
                line);
        
        renderer.fillRectangle(
                x + renderer.getTextWidth(MENU_WIDTH - 2),
                y + renderer.getTextHeight(pos) - 1, 
                renderer.getTextWidth(1), 
                line);
        renderer.restore();
    }
    
    public void tickTiles()
    {
        registeredTiles.values().forEach(tile -> tile.tick());
    }
}
