#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2015 Thomas Voegtlin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import jsonrpclib
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer, SimpleJSONRPCRequestHandler

from util import json_decode, DaemonThread
from wallet import WalletStorage, Wallet
from commands import known_commands, Commands
from simple_config import SimpleConfig


def get_daemon(config):
    host = config.get('rpchost', 'localhost')
    port = config.get('rpcport', 7777)
    server = jsonrpclib.Server('http://%s:%d' % (host, port))
    # check if daemon is running
    try:
        server.ping()
        return server
    except:
        pass


class RequestHandler(SimpleJSONRPCRequestHandler):

    def do_OPTIONS(self):
        self.send_response(200)
        self.end_headers()

    def end_headers(self):
        self.send_header("Access-Control-Allow-Headers",
                         "Origin, X-Requested-With, Content-Type, Accept")
        self.send_header("Access-Control-Allow-Origin", "*")
        SimpleJSONRPCRequestHandler.end_headers(self)



class Daemon(DaemonThread):

    def __init__(self, config, network, gui=None):
        DaemonThread.__init__(self)
        self.config = config
        self.network = network
        self.gui = gui
        self.wallets = {}
        if gui is None:
            self.wallet = self.load_wallet(config)
        else:
            self.wallet = None
        self.cmd_runner = Commands(self.config, self.wallet, self.network)
        host = config.get('rpchost', 'localhost')
        port = config.get('rpcport', 7777)
        self.server = SimpleJSONRPCServer((host, port), requestHandler=RequestHandler, logRequests=False)
        self.server.timeout = 0.1
        for cmdname in known_commands:
            self.server.register_function(getattr(self.cmd_runner, cmdname), cmdname)
        self.server.register_function(self.run_cmdline, 'run_cmdline')
        self.server.register_function(self.ping, 'ping')
        self.server.register_function(self.run_daemon, 'daemon')
        self.server.register_function(self.run_gui, 'gui')

    def ping(self):
        return True

    def run_daemon(self, config):
        sub = config.get('subcommand')
        assert sub in ['start', 'stop', 'status']
        if sub == 'start':
            response = "Daemon already running"
        elif sub == 'status':
            p = self.network.get_parameters()
            response = {
                'path': self.network.config.path,
                'server': p[0],
                'blockchain_height': self.network.get_local_height(),
                'server_height': self.network.get_server_height(),
                'nodes': self.network.get_interfaces(),
                'connected': self.network.is_connected(),
                'auto_connect': p[4],
                'wallets': self.wallets.keys(),
            }
        elif sub == 'stop':
            self.stop()
            response = "Daemon stopped"
        return response

    def run_gui(self, config_options):
        config = SimpleConfig(config_options)
        if self.gui:
            if hasattr(self.gui, 'new_window'):
                path = config.get_wallet_path()
                self.gui.new_window(path, config.get('url'))
                response = "ok"
            else:
                response = "error: current GUI does not support multiple windows"
        else:
            response = "Error: Electrum is running in daemon mode. Please stop the daemon first."
        return response

    def load_wallet(self, config):
        path = config.get_wallet_path()
        if path in self.wallets:
            wallet = self.wallets[path]
        else:
            storage = WalletStorage(path)
            wallet = Wallet(storage)
            wallet.start_threads(self.network)
            self.wallets[path] = wallet
        return wallet

    def run_cmdline(self, config_options):
        password = config_options.get('password')
        config = SimpleConfig(config_options)
        cmdname = config.get('cmd')
        cmd = known_commands[cmdname]
        wallet = self.load_wallet(config) if cmd.requires_wallet else None
        if wallet:
            wallet.wait_until_synchronized()
        # arguments passed to function
        args = map(lambda x: config.get(x), cmd.params)
        # decode json arguments
        args = map(json_decode, args)
        # options
        args += map(lambda x: config.get(x), cmd.options)
        cmd_runner = Commands(config, wallet, self.network)
        cmd_runner.password = password
        func = getattr(cmd_runner, cmd.name)
        result = func(*args)
        return result

    def run(self):
        while self.is_running():
            self.server.handle_request()

    def stop(self):
        for k, wallet in self.wallets.items():
            wallet.stop_threads()
        DaemonThread.stop(self)

@@ -729,16 +729,32 @@
 ses/%3E.%0A%0A
+import ast, os%0A%0A
 import j
@@ -1035,18 +1035,16 @@
 def 
-get_daemon
+lockfile
 (con
@@ -1057,88 +1057,205 @@
     
-host = config.get('rpchost', 'localhost')%0A    port = config.get('rpcport', 7777)
+return os.path.join(config.path, 'daemon')%0A%0Adef get_daemon(config):%0A    try:%0A        with open(lockfile(config)) as f:%0A            host, port = ast.literal_eval(f.read())%0A    except:%0A        return
 %0A   
@@ -2352,12 +2352,9 @@
 t', 
-7777
+0
 )%0A  
@@ -2457,16 +2457,123 @@
 =False)%0A
+        with open(lockfile(config), 'w') as f:%0A            f.write(repr(self.server.socket.getsockname()))%0A
         
@@ -5630,16 +5630,57 @@
 equest()
+%0A        os.unlink(lockfile(self.config))
 %0A%0A    de

use available port