import { Assets, Ticker } from 'pixi.js';
import { Meta, PixiStory, StoryFn } from '@pixi/storybook-renderer';
import ReelsMachine from 'src/slotMachines/ReelsMachine';
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 MockSystem from 'src/systems/MockSystem';
import MaskingSystem, { MaskingSystemSettings } from 'src/systems/MaskingSystem';
import { STATES } from 'src/states/ClassicReelsStateMachine';
import { SlotMachineConfig } from 'src/types/SlotMachineConfig';

const args = {
    enabled: true,
    width: 1920,
    height: 1080,
};

const meta: Meta<typeof MaskingSystem> = {
    title: 'Systems/MaskingSystem',
    tags: ['autodocs'],
    parameters: {
        docs: {
            story: {
                height: 600,
            },
            description: {
                component: 'Preview the rectangular reel viewport mask and toggle it with the provided controls. The story mirrors the base reel setup and forwards the knob values into `setSettings` so you can iterate on mask dimensions without leaving Storybook.',
            },
        },
    },
    argTypes: argTypes(args),
    args: getDefaultArgs(args),
};

export default meta;

export const MaskingSystemStory: StoryFn<typeof args> = (_params, context) => new PixiStory({
    context,
    init: async (view) =>
    {
        Assets.reset();
        await Assets.init({
            manifest: 'area69/manifest.json',
        });
        await Assets.loadBundle('configs');
        await Assets.loadBundle('symbols');

        const config: SlotMachineConfig = Assets.get('slot_machine_config');
        const machine = new ReelsMachine(config);
        machine.systems.masking = new MaskingSystem(machine);
        const spinningState = machine.stateMachine.states[STATES.SPINNING];
        if (!spinningState.systems.includes('masking'))
        {
            spinningState.systems.push('masking');
        }
        const container = new MachineContainer(machine, false);

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

        const maskSettings: Partial<MaskingSystemSettings> = {
            enabled: _params.enabled,
            width: _params.width,
            height: _params.height,
        };
        (machine.systems.masking as MaskingSystem).setSettings(maskSettings);

        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);
    }
});
