# -*- coding: utf-8 -*-
from __future__ import print_function

from datetime import datetime
import socket
import sys

from cheroot import wsgi


ALL_HOSTS = '0.0.0.0'
DEFAULT_HOST = ALL_HOSTS
DEFAULT_PORT = 8080
MAX_PORT_DELTA = 10

WELCOME = u' # Clay (by Lucuma labs)\n'
ADDRINUSE = u' ---- Address already in use. Trying another port...'
RUNNING_ON = u' * Running on http://%s:%s'
HOW_TO_QUIT = u' -- Quit the server with Ctrl+C --\n'

HTTPMSG = '500 Internal Error'


class Server(object):

    def __init__(self, clay):
        self.clay = clay
        app = RequestLogger(clay.app)
        self.dispatcher = wsgi.WSGIPathInfoDispatcher({'/': app})

    def run(self, host=DEFAULT_HOST, port=DEFAULT_PORT):
        port = port or self.clay.settings.get('port', DEFAULT_PORT)
        host = host or self.clay.settings.get('host', DEFAULT_HOST)
        max_port = port + MAX_PORT_DELTA
        print(WELCOME)
        return self._testrun(host, port, max_port)

    def _testrun(self, host, current_port, max_port):
        self.print_help_msg(host, current_port)
        try:
            return self._run_wsgi_server(host, current_port)
        except socket.error, e:
            current_port += 1
            if current_port > max_port:
                return
            print(ADDRINUSE)
            self._testrun(host, current_port, max_port)

    def _run_wsgi_server(self, host, port):
        self.server = self._get_wsgi_server(host, port)
        try:
            return self.start()
        except KeyboardInterrupt:
            self.stop()

    def _get_wsgi_server(self, host, port):
        return wsgi.WSGIServer((host, port), wsgi_app=self.dispatcher)

    def start(self):
        return self.server.start()

    def stop(self):
        self.server.stop()

    def print_help_msg(self, host, port):
        if host == ALL_HOSTS:
            print(RUNNING_ON % ('localhost', port))
            for ip in socket.gethostbyname_ex(socket.gethostname())[2]:  # pragma: no branch
                if ip.startswith('192.'):  # pragma: no branch
                    print(RUNNING_ON % (ip, port))
                    break
        print(HOW_TO_QUIT)


class RequestLogger(object):

    def __init__(self, application, **kw):
        self.application = application

    def log_request(self, environ, now=None):
        now = now or datetime.now()
        msg = [' ',
            now.strftime('%H:%M:%S'), ' | ',
            environ.get('REMOTE_ADDR', '?'), '  ',
            environ.get('REQUEST_URI', ''), '  ',
            '(', environ.get('REQUEST_METHOD', ''), ')',
            ]
        msg = ''.join(msg)
        print(msg)

    def __call__(self, environ, start_response):
        self.log_request(environ)
        try:
            return self.application(environ, start_response)
        except Exception, e:
            start_response(HTTPMSG, [('Content-type', 'text/plain')], sys.exc_info())
            raise


@@ -1910,156 +1910,59 @@
     
-for ip in socket.gethostbyname_ex(socket.gethostname())%5B2%5D:  # pragma: no branch%0A                if ip.startswith('192.'):  # pragma: no branch%0A    
+local_ip = get_local_ip()%0A            if local_ip:%0A
     
@@ -1993,16 +1993,22 @@
 G_ON %25 (
+local_
 ip, port
@@ -2014,34 +2014,8 @@
 t))%0A
-                    break%0A
     
@@ -2809,8 +2809,210 @@
  raise%0A%0A
+%0Adef get_local_ip():%0A    interfaces = socket.gethostbyname_ex(socket.gethostname())%5B-1%5D%0A    for ip in interfaces:%0A        if isinstance(ip, basestring) and ip.startswith('192.'):%0A            return ip%0A%0A

refactor get_local_ip