#!/usr/bin/env python2

##
# PyChat
# https://github.com/leosartaj/PyChat.git
#
# Copyright (c) 2014 Sartaj Singh
# Licensed under the MIT license.
##

# system imports
import sys
import os

# twisted imports
from twisted.internet import gtk2reactor
gtk2reactor.install() # install reactor for gui
from twisted.internet import reactor
from twisted.python import log
from twisted.internet import defer

# protocol
from PyChat.client.protocol.ChatClientFactory import ChatClientFactory
from PyChat.client.protocol.ChatClientProtocol import ChatClientProtocol

# Other imports
from PyChat.client.defer import * # import all the callbacks and errbacks
from PyChat.client.options import parse_args

def start_factory(options):
    """
    Starts the factory
    """
    deferred = defer.Deferred()
    factory = ChatClientFactory(options.name, deferred) # setting up the factory
    factory.protocol = ChatClientProtocol
    reactor.connectTCP(HOST, options.port, factory)
    return deferred

if __name__ == '__main__':
    options, HOST = parse_args() # parse the arguments

    # setup the factory
    deferred = start_factory(options)
    deferred.addBoth(stop_log) 
    deferred.addBoth(stop_reactor) 

    try:
        # Start Logging
        # Logs in pwd as pychat.log by default
        log.startLogging(open(options.log , 'a'))
    except Exception, e:
        print 'Cannot Start PyChat'
        print e
        sys.exit(1) # end program if invalid log file

    reactor.run()
