import javax.swing.text.DateFormatter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;

public class Game {

    private static Game instance;
    public Mouse mouse;
    public Cat cat;
    public GamingArea gArea;
    public BufferedWriter writer;

    private String NewFileName() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("dd-MM-YYYY_HHmmss");
        return "./protokolle/" + df.format(LocalDateTime.now()) + ".txt";
    }

    private Game(int width, int height) throws IOException {
        gArea = new GamingArea(height, width);

        String filename = NewFileName();

        File f = new File(filename);
        try {
            boolean ok = f.createNewFile();
            if (!ok) {
                System.out.println("Cannot create file");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        writer = new BufferedWriter(new FileWriter(new File(filename)));

        initialisePlayers();
    }


    public static Game getInstance(int width, int height) throws IOException {
        if (instance == null)
        {
            instance = new Game(width, height);
        }
        return instance;
    }

    private void initialisePlayers() throws IOException {
        mouse = new Mouse(new Position(4,4), gArea, this);
        cat = new Cat(new Position(0,0), gArea, this);
    }
    public boolean gameOver()
    {
        if(mouse.positionPlayer.equals(cat.positionPlayer))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void printField()
    {
        int rowNum = 0;
        System.out.print("  ");
        for(int colNum = 0; colNum < 10; colNum++ )
        {
            System.out.print(colNum + " ");
        }
        System.out.println();
        Position[][] field = gArea.getPlayingField();
        for (Position[] row : field)
        {
            System.out.print(rowNum + " ");
            rowNum++;

            for (Position col : row)
            {
                System.out.print(col.displayChar + " ");
            }
            System.out.println();
        }
    }
}
