import { location_service } from './Location.service.js'
import { resource_service } from './Resource.service.js'
import { event_bus } from './EventBus.service.js'

const pageMap = {
    'dash.profile': '/dash/profile',
    'app.setup': '/dash/app/setup',
}

class ActionService {
    async perform({ text = '', action, ...args }) {
        if ( action === 'redirect' ) {
            if ( args.next ) {
                return location_service.redirect(args.next, args.delay || 0)
            }
        } else if ( action === 'navigate' ) {
            if ( args.page && pageMap[args.page] ) {
                window.history.pushState('pageNavigate', `Open ${args.page}`, pageMap[args.page])
                return event_bus.event('root.navigate').fire(args)
            }
        } else if ( action === 'back' ) {
            return location_service.back()
        } else if ( args.type === 'resource' ) {
            const { resource } = args
            if ( action === 'insert' ) {
                window.history.pushState('cobaltForm', `Insert ${resource}`, `/dash/c/form/${resource}`)

                return event_bus.event('root.navigate').fire({
                    page: 'cobalt.form',
                    resource,
                    mode: 'insert',
                })
            } else if ( action === 'update' ) {
                const { id } = args
                window.history.pushState('cobaltForm', `Edit ${resource}`, `/dash/c/form/${resource}?id=${id}`)

                return event_bus.event('root.navigate').fire({
                    page: 'cobalt.form',
                    resource,
                    mode: 'update',
                    form_id: id,
                })
            } else if ( action === 'delete' ) {
                const { id } = args
                const rsc = await resource_service.get(resource)
                await rsc.delete(id)
            } else if ( action === 'list' ) {
                window.history.pushState('cobaltListing', `View ${resource}`, `/dash/c/listing/${resource}`)

                return event_bus.event('root.navigate').fire({
                    page: 'cobalt.listing',
                    resource,
                })
            }
        } else if ( action === 'post' ) {
            const inputs = []

            if ( args.params ) {
                for (const param in args.params) {
                    if ( !args.params.hasOwnProperty(param) ) continue
                    inputs.push(`<input type="hidden" name="${param}" value="${args.params[param]}"/>`)
                }
            }

            const form_attrs = ['method="POST"']
            if ( args.destination ) {
                form_attrs.push(`action="${args.destination}"`)
            }

            $(`
            <form ${form_attrs.join(' ')}>
                ${inputs.join('\n')}
            </form>
            `).appendTo('body').submit()
        } else {
            throw new TypeError(`Unknown action type: ${action}`)
        }
    }
}

const action_service = new ActionService()
export { action_service }
