package net.ranides.test.adapter.model0;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Screen0 extends AbstractScreen0 implements Serializable {

    private int w;

    private int h;

    private List<Image0> images = new ArrayList<>();

    public Screen0(int w, int h) {
        this.w = w;
        this.h = h;
    }

    @Override
    public int width() {
        return w;
    }

    @Override
    public int height() {
        return h;
    }

    public Image0 put(String id, int x, int y) {
        Image0 out = new Image0(id, x, y);
        images.add(out);
        return out;
    }

    public Image0 put(Image0 image) {
        return put(image.getId(), image.getX(), image.getY());
    }

    @Override
    public void add(String id, int x, int y) {
        put(id, x, y);
    }

    @Override
    public void add(Image0 image) {
        put(image);
    }

    public boolean contains(Image0 image) {
        return images.stream().anyMatch(image::equals);
    }

    public String findId(int x, int y) {
        return images.stream().filter(m -> m.atLocation(x,y)).findFirst().map(m -> m.getId()).orElse("?");
    }

    protected Image0 findImage(int x, int y) {
        return images.stream().filter(m -> m.atLocation(x,y)).findFirst().orElse(new Image0("?",0,0));
    }

    protected Screen0 resize(double f) {
        w *= f;
        h *= f;
        for(Image0 i : images) {
            i.setX((int)(i.getX() * f));
            i.setY((int)(i.getY() * f));
        }
        return this;
    }

    @Override
    public String toString() {
        return "Screen0{" + w +"x" + h + "}";
    }
}
