import { CONFIG } from './js/_config.js';
import { cam } from './js/cam.js';
import { GameObject } from './js/gameObject.js';
import { anthill } from './js/anthill.js';
import { Ant, updateAnts, drawAnts } from './js/ant.js';
import { controls } from './js/controls.js';
import { gameTime } from './js/gameTime.js';
import { PauseScreen } from './js/pause.js';
import { resources } from './js/resource.js';
import { Signalant, drawSignalants } from './js/signalant.js';
import { background } from './js/background.js';
import { ui } from './js/ui.js';
import { storage } from './js/storage.js';
import { currency } from './js/currency.js';
import { music } from './js/music.js';
import { sfx } from './js/sfx.js';

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let renderScale = 1;
const pauseScreen = new PauseScreen(canvas);

function resizeCanvas() {
    const width = window.innerWidth;
    const height = window.innerHeight;

    renderScale = window.devicePixelRatio || 1;
    canvas.width = Math.round(width * renderScale);
    canvas.height = Math.round(height * renderScale);
    canvas.style.width = `${width}px`;
    canvas.style.height = `${height}px`;

    ctx.setTransform(renderScale, 0, 0, renderScale, 0, 0);
    ctx.imageSmoothingEnabled = false;
    cam.setHome(width / 4, height / 4);
}

document.addEventListener("DOMContentLoaded", resizeCanvas);
window.addEventListener("resize", resizeCanvas);

resizeCanvas();

cam.home();

// after 0.5 seconds, spawn test signalants to demonstrate the signalant functionality
setTimeout(() => {
    //storage.addSignalant(new Signalant("test_signalant_1", { toStorage: true }));
    /*
    storage.addSignalant(new Signalant("test_signalant_1", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_2", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_3", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_4", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_5", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_6", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_7", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_8", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_9", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_10", { toStorage: true }));
    storage.addSignalant(new Signalant("test_signalant_11", { toStorage: true }));
    */

    //new Signalant("test_signalant_0", { x: 180, y: 130, direction: 30, affectionRadius: 50, affectsColors: ["red", "red", "red"], affectedColorsDirections: { red: [40, 80, 320] }, isFixed: true });
}, 500);

document.title = CONFIG.GAME_TITLE;

function main() {
    window.requestAnimationFrame(main);
    gameTime.update();
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.setTransform(renderScale, 0, 0, renderScale, 0, 0);
    ctx.imageSmoothingEnabled = false;

    if (!pauseScreen.isActive()) {
        const uiState = ui.update();
        music.update();
        sfx.update({
            isPheromoneAntMoving: uiState.isPheromoneAntMoving,
            isQueenMoving: uiState.isQueenMoving
        });
        anthill.spawn();
        resources.update();
        cam.update({ allowPan: uiState.allowCameraPan, allowZoom: uiState.allowCameraZoom });
        updateAnts();
        anthill.update({ allowRightClick: uiState.allowQueenRightClick });
    } else {
        sfx.update({ isPheromoneAntMoving: false, isQueenMoving: false });
        ui.reposition();
    }

    background.draw(ctx, cam, { width: canvas.width / renderScale, height: canvas.height / renderScale });
    anthill.draw(ctx, cam);
    resources.draw(ctx, cam);
    drawAnts(ctx, cam);
    drawSignalants(ctx, cam);
    ui.draw(ctx);
    pauseScreen.draw(ctx);

    controls.resetFrameState();
}

document.addEventListener("DOMContentLoaded", function() {
    main();
});