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

const args = {
    accelerationTime: 1,
    backCoef: 1.1,
    maxSpeed: 40,
    reelStartDelay: 0.2,
    reelStopDelay: 0.5,
    spinningDuration: 2,
    blur: true,
    bounce: true
};

const meta: Meta<typeof ReelsSpinSystem> = {
    title: 'Systems/ReelsSpinSystem', // Путь для отображения в Storybook
    tags: ['autodocs'], // Включает автоматическую генерацию документации
    parameters: {
        docs: {
            story: {
                height: 600
            },
            description: {
                component: 'Controls the spinning behavior of slot machine reels, including acceleration, sustained spinning, and deceleration. This class manages reel state transitions, timing for each stage of the spin, and the display of symbols on each reel.'
            },
        }
    },
    argTypes: argTypes(args),
    args: getDefaultArgs(args),
};

export default meta;

export const ReelsSpinSystemStory: 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);
        machine.stateMachine.states[STATES.SPINNING].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);
        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);
    }
});

export const ReelsSpinSystemPreventedReelsStory: 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);
        machine.stateMachine.states[STATES.SPINNING].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);
        machine.systems.spin.setSettings(_params);

        // Disable first and last reels from spinning
        const reelsCount = config.reels;
        machine.maps.preventReelsSpin = Array.from({ length: reelsCount }, (_, i) => i === 0 || i === reelsCount - 1);

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