/*
 * Project: ImpishExploration
 * Module: Placeholder_Game-Core_main
 * File: GuiTextList.java
 *
 * Created by Tim on 11/06/17 13:54 on behalf of SmithsGaming
 * Copyright (c) 2017. All rights reserved.
 *
 * Last modified 11/06/17 13:48
 */

package com.scoutgamestudio.placeholder.client.gui.components;

import com.scoutgamestudio.placeholder.client.Client;
import com.scoutgamestudio.placeholder.client.GameClient;
import com.scoutgamestudio.placeholder.client.gui.GuiComponent;
import com.scoutgamestudio.placeholder.client.util.MouseButtonEvent;
import com.scoutgamestudio.placeholder.client.util.MouseMoveEvent;
import com.scoutgamestudio.placeholder.common.util.GraphicUtils;
import com.scoutgamestudio.placeholder.common.util.maths.Vector2d;

import java.awt.*;
import java.util.ArrayList;

/**
 * Created by Tim on 07/03/2016.
 */
public class GuiTextList extends GuiComponent implements ISliderListener {

    private final ArrayList<String> textItems;
    protected int height;
    protected int width;
    private GuiSliderVertical guiSlider;
    private int itemSelected = -1;
    private int maxDisplayed;
    private int displayIndex = 0;

    public GuiTextList(Vector2d position, ArrayList textItems, int width, int height) {
        super(position);
        if (textItems != null) {
            this.textItems = textItems;
        } else {
            this.textItems = new ArrayList<>();
        }
        this.width = width;
        this.height = height;
        guiSlider = new GuiSliderVertical(0, new Vector2d(width - 17, 1), this, 15, height - 3, 0, 1);
        maxDisplayed = (height - 2) / Client.getGameClient().getFontMetrics(GameClient.defaultFont).getHeight();
    }

    @Override
    public void setOffset(Vector2d offset) {
        super.setOffset(offset);
        this.containedArea = new Rectangle((int) position.x + (int) offset.x, (int) position.y + (int) offset.y, width, height);
        guiSlider.setOffset(new Vector2d(position.x + offset.x, position.y + offset.y));
    }

    @Override
    public void update() {
    }

    @Override
    public void render(Graphics g) {
        g.setColor(Color.DARK_GRAY);
        g.fillRect((int) position.x + (int) offset.x, (int) position.y + (int) offset.y, width, height);
        g.setColor(new Color(65, 65, 65));
        g.fillRect((int) position.x + (int) offset.x + 1, (int) position.y + (int) offset.y + 1, width - 2, height - 2);
        g.setFont(GameClient.defaultFont);
        g.setColor(Color.WHITE);
        int spacing = g.getFontMetrics().getHeight();
        int y = (int) this.offset.y + (int) this.position.y + spacing / 2;
        if (!textItems.isEmpty()) {
            for (int i = displayIndex; i < displayIndex + maxDisplayed; i++) {
                if (textItems.size() > i) {
                    GraphicUtils.drawCenteredString(g, GraphicUtils.trimStringToLength(g, textItems.get(i), GameClient.defaultFont, width - 20), GameClient.defaultFont, (int) (position.x + width / 2), (int) position.y + y);
                    y += spacing;
                } else {
                    break;
                }
            }
        }
        if (itemSelected >= 0) {
            g.setColor(Color.WHITE);
            g.drawRect((int) position.x + (int) offset.x + 1, (int) position.y + (int) offset.y + 1 + spacing * itemSelected, width - 20, spacing);
        }
        guiSlider.render(g);
    }

    public String getSelectedText() {
        if (itemSelected >= 0) {
            return textItems.get(itemSelected + displayIndex);
        }
        return null;
    }

    public void addString(String string) {
        textItems.add(string);
    }

    @Override
    public void mousePressed(MouseButtonEvent e) {
        guiSlider.mousePressed(e);
    }

    @Override
    public void mouseReleased(MouseButtonEvent e) {
        guiSlider.mouseReleased(e);
    }

    @Override
    public void mouseClicked(MouseButtonEvent e) {
        guiSlider.mouseClicked(e);
        if (!guiSlider.getContainedArea().contains(e.x, e.y)) {
            int yRelative = e.y - (int) position.y - (int) offset.y;
            int index = yRelative / Client.getGameClient().getFontMetrics(GameClient.defaultFont).getHeight();
            if (index < textItems.size()) {
                itemSelected = index;
            }
        }
    }

    @Override
    public void mouseMoved(MouseMoveEvent e) {
        guiSlider.mouseMoved(e);
    }

    @Override
    public void mouseDragged(MouseMoveEvent e) {
        guiSlider.mouseDragged(e);
    }

    @Override
    public void sliderMoved(int id, GuiSlider slider, float amount) {
        itemSelected = -1;
        float itemSize = 1F / textItems.size();
        if (textItems.size() > maxDisplayed) {
            displayIndex = (int) (amount / itemSize);
        }
    }
}