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

const args = {
    gravity: 4000,
    reelDelay: 0.12,
    slotDelay: 0.03,
    refillDelay: 0.5,
};

const meta: Meta<typeof CascadeSpinSystem> = {
    title: 'Systems/CascadeSpinSystem', // Путь для отображения в Storybook
    tags: ['autodocs'], // Включает автоматическую генерацию документации
    parameters: {
        docs: {
            story: {
                height: 600
            },
            description: {
                component: 'Manages the cascade spinning effect in the slot machine. Handles dropping, refilling, and bouncing symbols with gravity effects.'
            },
        }
    },
    argTypes: argTypes(args),
    args: getDefaultArgs(args),
};

export default meta;

export const CascadeSpinSystemStory: 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 CascadeMachine(config);
        const container = new MachineContainer(machine);
        machine.systems.blockExitState = new MockSystem(machine)
        view.addChild(container);
        context.machine = machine;
        machine.inputs.initMap = createScreenInputMap(config);
        machine.inputs.screenMap = createScreenInputMap(config);
        machine.systems.spin.setSettings(_params);
        machine.inputs.spin = true;
        centerView(view);
    },
    resize: centerView,
    update: () =>
    {
        context.machine && context.machine.update(Ticker.shared.deltaMS);
    }
});
