# (c) 2016, Hao Feng <whisperaven@gmail.com>

import cherrypy

from .utils import *
from .consts import *
from .handler import EndpointHandler

from exe.runner import ServiceRunner


def _state_parse(state):
    restart = False
    if state == STATE_STARTED:
        start = True
    elif state == STATE_STOPED:
        start = False
    elif state == STATE_RESTARTED:
        start = True
        restart = True
    else:
        raise cherrypy.HTTPError(status.BAD_REQUEST, ERR_BAD_SERVPARAMS)
    return start, restart


@cherrypy.expose
class ServiceHandler(EndpointHandler):
    """ Endpoint Handler: ``/service``. """

    __RUNNER__ = ServiceRunner

    @cherrypy.tools.json_out()
    def GET(self, **params):
        """ Manipulate service on remote host. """
        target = parse_params_target(params)

        name = params.pop('name', "")
        state = parse_params_int(params, 'state')
        if not name or state == None:
            raise cherrypy.HTTPError(status.BAD_REQUEST, ERR_BAD_SERVPARAMS)

        name = name.lower()
        start, restart = _state_parse(state)
        graceful = parse_params_bool(params, 'graceful')

        result = self.handle(target, name, start, restart, graceful)
        if not result:
            raise cherrypy.HTTPError(status.NOT_FOUND, ERR_NO_MATCH)
        else:
            return api_response(status.OK, result)

    @cherrypy.tools.json_in(force=False)
    @cherrypy.tools.json_out()
    def POST(self):
        """ Manipulate service on remote host(s). """
        targets = cherrypy.request.json.pop('targets', [])
        if not targets or not isinstance(targets, list):
            raise cherrypy.HTTPError(status.BAD_REQUEST, ERR_NO_TARGET)

        name = cherrypy.request.json.pop('name', "")
        state = cherrypy.request.json.pop('state', None)
        if not name or not isinstance(name, str) or state == None:
            raise cherrypy.HTTPError(status.BAD_REQUEST, ERR_BAD_SERVPARAMS)

        name = name.lower()
        start, restart = _state_parse(state)
        graceful = cherrypy.request.json.pop('graceful', False)

        jid = self.handle(targets, name, start, restart,
                          graceful, run_async=True)
        return api_response(status.CREATED, dict(jid=jid))
