import { Assets, Ticker } from 'pixi.js';
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 ReelsMachine from '../slotMachines/ReelsMachine';
import { STATES } from '../states/ClassicReelsStateMachine';
import AnticipationSystem from '../systems/AnticipationSystem';
import AnticipationAnimationsSystem from "../systems/AnticipationAnimationsSystem";
import AnticipationBack from "../entities/AnticipationBack";
import AnticipationFront from "../entities/AnticipationFront";
import MockSystem from "src/systems/MockSystem";

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/AnticipationSystem', // Путь для отображения в Storybook
    tags: ['autodocs'], // Включает автоматическую генерацию документации
    parameters: {
        docs: {
            story: {
                height: 600
            },
            description: {
                component: 'Anticipation of landing symbols',
            },
        }
    },
    argTypes: argTypes(args),
    args: getDefaultArgs(args),
};

export default meta;

export const AnticipationSystemStory: 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);
        const container = new MachineContainer(machine);
        machine.systems.blockExitState = new MockSystem(machine)
        view.addChild(container);
        context.machine = machine;
        machine.systems.spin.setSettings(_params);
        machine.systems.anticipation1 = new AnticipationSystem(machine);
        machine.systems.anticipationAnim = new AnticipationAnimationsSystem(machine);
        machine.systems.anticipationAnim.setSettings({
            entityClass: AnticipationBack,
            zIndex: 0
        });
        machine.systems.anticipationAnimFront = new AnticipationAnimationsSystem(machine);
        machine.systems.anticipationAnimFront.setSettings({
            entityClass: AnticipationFront,
            zIndex: 10
        });
        machine.inputs.initMap = createScreenInputMap(config);
        machine.inputs.screenMap = createScreenInputMap(config)
            .map((record, index) => ([0, 1].includes(index) ? { ...record, key: 'A' } : record))
            .map((record, index) => ([5].includes(index) ? { ...record, key: 'B' } : record));

        // machine.systems.anticipation1.setSettings({ anticipatingSymbolKey: 'A', preventReelStopIndex: 0 });
        // machine.systems.anticipation2 = new AnticipationSystem(machine);
        // machine.systems.anticipation2.setSettings({ anticipatingSymbolKey: ['B', 'C'], preventReelStopIndex: 1 });
        // machine.stateMachine.states[STATES.SPINNING].systems = ['anticipation1', 'anticipation2', 'spin',];
        machine.stateMachine.states[STATES.SPINNING].systems = ['anticipation1', "anticipationAnim", "anticipationAnimFront", 'spin'];
        machine.inputs.spin = true;
        machine.inputs.blockExitState = true;
        setTimeout(() => {
            machine.inputs.blockExitState = false;
        }, 250);
        centerView(view);
        centerView(view);
    },
    resize: centerView,
    update: () =>
    {
        context.machine && context.machine.update(Ticker.shared.deltaMS);
    }
});
