import { createMachine, interpret,assign } from "xstate";

export const machine = createMachine(
  {
    id: "trackTime",
    initial: "choose",
    context:{
        currentStatus:'',
        checkData:null
    },
    on: {
        RESET:{
            target:'.choose',
            actions:assign(_=>{
                return {
                    currentStatus:''
                }
            })
        }
    },
    states: {
      choose: {
        on: {
          SLEEP: {
            target: "SLEEP"
          },
          FAST_SLEEP: {
            target: "FAST_SLEEP"
          },
          FAST: {
            target: "FAST"
          },
        },
      },
      SLEEP: {
        initial: "WAIT_FOR_START",
        states: {
          WAIT_FOR_START: {
            on: {
              START_SLEEP: {
                target: "ONGOING"
              },
            },
          },
          ONGOING: {
            on: {
              END_SLEEP: {
                target: "COMPLETED"
              },
            },
          },
          COMPLETED: {},
        },
      },
      FAST_SLEEP: {
        initial: "WAIT_FOR_START",
        states: {
          WAIT_FOR_START: {
            on: {
              START_FAST: {
                target: "ONGOING1"
              },
            },
          },
          ONGOING1: {
            on: {
              START_SLEEP: {
                target: "ONGOING2"
              },
              END_FAST: {
                target: "COMPLETED"
              },
            },
          },
          ONGOING2: {
            on: {
              END_FAST: {
                target: "COMPLETED"
              },
              END_SLEEP: {
                target: "ONGOING3"
              },
            },
          },
          COMPLETED: {},
          ONGOING3: {
            on: {
              END_FAST: {
                target: "COMPLETED"
              },
            },
          },
        },
      },
      FAST: {
        initial: "WAIT_FOR_START",
        states: {
          WAIT_FOR_START: {
            on: {
              START_FAST: {
                target: "ONGOING"
              },
            },
          },
          ONGOING: {
            on: {
              END_FAST: {
                target: "COMPLETED"
              },
            },
          },
          COMPLETED: {},
        },
      },
    },
    schema: {
      events: {} as
        | { type: "FAST" }
        | { type: "FAST_SLEEP" }
        | { type: "SLEEP" }
        | { type: "END_FAST" }
        | { type: "END_SLEEP" }
        | { type: "START_FAST" }
        | { type: "START_SLEEP" }
        | { type: "RESET" },
    },
    predictableActionArguments: true,
    preserveActionOrder: true,
  },
  {
    actions: {
        apple:assign({
        }),
        setCurrentStatus:assign((context, event) => {
            return {
                currentStatus: event.type
            }
        })
    },
    services: {},
    guards: {},
    delays: {},
  },
);

export  const setSpecifiedState = (currentState, specifiedState) => {
    return assign({
      myMachine: {
        ...currentState.myMachine,
        value: specifiedState,
      },
    });
  };

export const setSpecifiedStatus = (currentState: any, specifiedState: any) => {
    return assign({
        machine:{
            currentState,
            specifiedState
        }
    })
}

const service = interpret(machine).start();
// const service = machine.withContext(initialContext).start();

export default service;
