package cz.jzitnik.events;

import com.googlecode.lanterna.TerminalPosition;
import com.googlecode.lanterna.TerminalSize;
import cz.jzitnik.utils.events.Event;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.awt.*;

public record RerenderScreen(ScreenPart[] parts) implements Event {
    public RerenderScreen(ScreenPart part) {
        this(new ScreenPart[]{part});
    }

    public static RerenderScreen full(TerminalSize terminalSize) {
        return new RerenderScreen(new ScreenPart[]{ScreenPart.full(terminalSize)});
    }

    @Data
    @AllArgsConstructor
    public static class ScreenPart {
        private TerminalPosition start;
        private TerminalPosition end;

        public static ScreenPart full(TerminalSize terminalSize) {
            return new ScreenPart(
                    new TerminalPosition(0, 0),
                    new TerminalPosition(terminalSize.getColumns() - 1, terminalSize.getRows() * 2 - 1)
            );
        }

        public boolean isWithin(TerminalPosition terminalPosition) {
            return
                    terminalPosition.getColumn() >= start.getColumn() &&
                            terminalPosition.getColumn() <= end.getColumn() &&
                            terminalPosition.getRow() >= start.getRow() &&
                            terminalPosition.getRow() <= end.getRow();
        }
    }
}
