package gui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import gui.render.DrawboxRectengleRenderingFunction;
import gui.render.HitboxCircleRenderingFunction;
import gui.render.HitboxRectengleRenderingFunction;
import gui.render.ShapeRenderingFunction;
import model.Entity;
import repository.Project;

public class EditableCanvas extends JPanel implements MouseListener, MouseMotionListener {

	DrawboxRectengleRenderingFunction drawboxRectengleRenderFunct;
	HitboxRectengleRenderingFunction hitboxRectengleRenderFunct;
	HitboxCircleRenderingFunction hitboxCircleRenderFunct;
	ShapeRenderingFunction renderingFunction;
	Entity entity;
	BufferedImage image;
	
	
	public EditableCanvas() {
		drawboxRectengleRenderFunct = new DrawboxRectengleRenderingFunction();
		hitboxRectengleRenderFunct = new HitboxRectengleRenderingFunction();
		hitboxCircleRenderFunct = new HitboxCircleRenderingFunction();
		drawboxRectengleRenderFunct.subscribe(Project.getInstance()); // подписка: Project получит данные ввиде обьекта Event, содержащий данные drawbox при отрисовке последней точки из 4-х.
	    hitboxRectengleRenderFunct.subscribe(Project.getInstance());
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	
	public HitboxRectengleRenderingFunction getHitboxRectengleRenderFunct() {
		return hitboxRectengleRenderFunct;
	}

	public void drawing(Graphics2D g) {
		if(renderingFunction!=null&&entity!=null) {
			renderingFunction.drawing(g);
		}
	}
	
	public void setDrawboxRectengleRenderingFunction() {
		renderingFunction = drawboxRectengleRenderFunct;
	}	
	public void setHitboxRectengleRenderingFunction() {
		renderingFunction = hitboxRectengleRenderFunct;
	}	
	public void setHitboxCircleRenderingFunction() {
		renderingFunction = hitboxCircleRenderFunct;
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		//System.out.println("moved");
		if(renderingFunction!=null&&entity!=null) {
			//System.out.println("x="+e.getX()+"y="+e.getY());
			renderingFunction.mouseMoved(e);
			repaint();
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		//System.out.println("mousePressed");
		if(renderingFunction!=null&&entity!=null) {
			renderingFunction.mousePressed(e);
			repaint();
		}
	}	

	
   @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this); 
        if(entity != null) {
        	drawing((Graphics2D)g);
        }
    }
	
   public void setEntity(Entity e) {
		entity = e;
		drawboxRectengleRenderFunct.setEntityInDrawboxRectengle(e);
		hitboxRectengleRenderFunct.setEntityInHtiboxRectengle(e);
	}
	
   public void setImage(BufferedImage image) {
	   this.image = image;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {}
	@Override
	public void mouseDragged(MouseEvent e) {}
	@Override
	public void mouseReleased(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}



}
