-- From https://github.com/unindented/lua-fsm/blob/master/src/fsm.lua
--[[
	Copyright (c) 2016 Daniel Perez Alvarez

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
]] --
local Fsm = {}
Fsm.WILDCARD = "*"
Fsm.DEFERRED = 0
Fsm.SUCCEEDED = 1
Fsm.NO_TRANSITION = 2
Fsm.PENDING = 3
Fsm.CANCELLED = 4
local function do_callback(handler, args)
	if handler then
		return handler(unpack(args))
	end
end
local function before_event(self, event, _, _, args)
	local specific = do_callback(self["on_before_" .. event], args)
	local general = do_callback(self["on_before_event"], args)
	if specific == false or general == false then
		return false
	end
end
local function leave_state(self, _, from, _, args)
	local specific = do_callback(self["on_leave_" .. from], args)
	local general = do_callback(self["on_leave_state"], args)
	if specific == false or general == false then
		return false
	end
	if specific == Fsm.DEFERRED or general == Fsm.DEFERRED then
		return Fsm.DEFERRED
	end
end
local function enter_state(self, _, _, to, args)
	do_callback(self["on_enter_" .. to] or self["on_" .. to], args)
	do_callback(self["on_enter_state"] or self["on_state"], args)
end
local function after_event(self, event, _, _, args)
	do_callback(self["on_after_" .. event] or self["on_" .. event], args)
	do_callback(self["on_after_event"] or self["on_event"], args)
end
local function build_transition(self, event, states)
	return function(...)
		local from = self.current
		local to = states[from] or states[Fsm.WILDCARD] or from
		local args = {self, event, from, to, ...}
		assert(not self.is_pending(), "previous transition still pending")
		assert(self.can(event), "invalid transition from state '" .. from .. "' with event '" .. event .. "'")
		local before = before_event(self, event, from, to, args)
		if before == false then
			return Fsm.CANCELLED
		end
		if from == to then
			after_event(self, event, from, to, args)
			return Fsm.NO_TRANSITION
		end
		self.confirm = function()
			self.confirm = nil
			self.cancel = nil
			self.current = to
			enter_state(self, event, from, to, args)
			after_event(self, event, from, to, args)
			return Fsm.SUCCEEDED
		end
		self.cancel = function()
			self.confirm = nil
			self.cancel = nil
			after_event(self, event, from, to, args)
			return Fsm.CANCELLED
		end
		local leave = leave_state(self, event, from, to, args)
		if leave == false then
			return Fsm.CANCELLED
		end
		if leave == Fsm.DEFERRED then
			return Fsm.PENDING
		end
		if self.confirm then
			return self.confirm()
		end
	end
end
function Fsm.create(cfg, target)
	local self = target or {}
	-- Initial state.
	local initial = cfg.initial
	-- Allow for a string, or a map like `{state = "foo", event = "setup"}`.
	initial = type(initial) == "string" and {state = initial} or initial
	-- Initial event.
	local initial_event = initial and initial.event or "startup"
	-- Terminal state.
	local terminal = cfg.terminal
	-- Events.
	local events = cfg.events or {}
	-- Callbacks.
	local callbacks = cfg.callbacks or {}
	-- Track state transitions allowed for an event.
	local states_for_event = {}
	-- Track events allowed from a state.
	local events_for_state = {}
	local function add(e)
		-- Allow wildcard transition if `from` is not specified.
		local from = type(e.from) == "table" and e.from or (e.from and {e.from} or {Fsm.WILDCARD})
		local to = e.to
		local event = e.name
		states_for_event[event] = states_for_event[event] or {}
		for _, fr in ipairs(from) do
			events_for_state[fr] = events_for_state[fr] or {}
			table.insert(events_for_state[fr], event)
			-- Allow no-op transition if `to` is not specified.
			states_for_event[event][fr] = to or fr
		end
	end
	if initial then
		add({name = initial_event, from = "none", to = initial.state})
	end
	for _, event in ipairs(events) do
		add(event)
	end
	for event, states in pairs(states_for_event) do
		self[event] = build_transition(self, event, states)
	end
	for name, callback in pairs(callbacks) do
		self[name] = callback
	end
	self.current = "none"
	function self.is(state)
		if type(state) == "table" then
			for _, s in ipairs(state) do
				if self.current == s then
					return true
				end
			end
			return false
		end
		return self.current == state
	end
	function self.can(event)
		local states = states_for_event[event]
		local to = states[self.current] or states[Fsm.WILDCARD]
		return to ~= nil
	end
	function self.cannot(event)
		return not self.can(event)
	end
	function self.transitions()
		return events_for_state[self.current]
	end
	function self.is_pending()
		return self.confirm ~= nil
	end
	function self.is_finished()
		return self.is(terminal)
	end
	if initial and not initial.defer then
		self[initial_event]()
	end
	return self
end
return Fsm