import { Assets, Ticker } from 'pixi.js';
import { Meta, PixiStory, StoryFn } from '@pixi/storybook-renderer';
import StaticReelsMachine from '../slotMachines/StaticReelsMachine';
import { argTypes, getDefaultArgs } from '../stories/utils/argTypes';
import { createScreenInputMap } from '../stories/utils/createScreenInputMap';
import MachineContainer from '../stories/utils/MachineContainer';
import { centerView } from '../stories/utils/resize';
import StaticReelsSpinSystem from '../systems/StaticReelsSpinSystem';
import { SlotMachineConfig } from '../types/SlotMachineConfig';
import MockSystem from "src/systems/MockSystem";

const args = {
    reelDelay: 0.1,
    slotDelay: 0.03,
    refillDelay: 0.15,
};

const meta: Meta<typeof StaticReelsSpinSystem> = {
    title: 'Systems/StaticReelsSpinSystem',
    tags: ['autodocs'],
    parameters: {
        docs: {
            story: {
                height: 600
            },
            description: {
                component: 'Add doc'
            },
        }
    },
    argTypes: argTypes(args),
    args: getDefaultArgs(args),
};

export default meta;

export const StaticReelsSpinSystemStory: StoryFn<typeof args> = (_params, context) => new PixiStory({
    context, init: async (view) =>
    {
        Assets.reset();
        await Assets.init({
            manifest: 'bubblex/manifest.json',
        });
        await Assets.loadBundle('configs');
        await Assets.loadBundle('symbols');
        const config: SlotMachineConfig = Assets.get('slot_machine_config');
        const machine = new StaticReelsMachine(config);
        const container = new MachineContainer(machine);

        view.addChild(container);
        context.machine = machine;
        machine.systems.blockExitState = new MockSystem(machine)
        machine.inputs.initMap = createScreenInputMap(config);
        machine.inputs.screenMap = createScreenInputMap(config);
        machine.systems.spin.setSettings(_params);
        machine.inputs.spin = true;

        machine.inputs.blockExitState = true;
        setTimeout(() => {
            machine.inputs.blockExitState = false;
        }, 250);
        centerView(view);
    },
    resize: centerView,
    update: () =>
    {
        context.machine && context.machine.update(Ticker.shared.deltaMS);
    }
});
