#!/usr/bin/python

import sys
import Skype4Py
import os
from subprocess import *
from optparse import OptionParser

class SkypeHandler(Skype4Py.Skype):
    # Change these to match your screen resolution
    screen_width = 1920
    screen_height = 1080

    def __init__(self):
        Skype4Py.Skype.__init__(self)
    
        # Here we define a set of call statuses that indicate a call has been either aborted or finished
        CallIsFinished = set ([Skype4Py.clsFailed, Skype4Py.clsFinished, Skype4Py.clsMissed, Skype4Py.clsRefused, Skype4Py.clsBusy, Skype4Py.clsCancelled]);


	# Starting Skype if it's not running already..
	if not self.Client.IsRunning:
	    print 'Starting Skype..'
            self.Client.Start()

        # Attatching to Skype..
        print 'Connecting to Skype..'
        self.Attach()

    def answer_call(self, option, opt_str, value, parser):
        answered = False
        for call in self.ActiveCalls:
            if call.Status == Skype4Py.clsRinging:
                self.Call(call.Id).Answer()	
                answered = True

        if not answered:
            print "No ringing calls deteced"

    def end_call(self, option, opt_str, value, parser):
        finished = False
        for call in self.ActiveCalls:
            self.Call(call.Id).Finish()
            finished = True

        if not finished:
            print "No active calls found to finish"

    def maximise_video(self, option, opt_str, value, parser):
        print "Maximising window"
        cmd = "/usr/bin/xdotool"
        
        arg1 = "search"
        arg2 = "--title"
        arg3 = "Call with"
        win_id = Popen([cmd, arg1, arg2, arg3], stdout=PIPE).communicate()[0]

        print win_id
        arg1 = "windowactivate"
        arg2 = win_id
        win_id = Popen([cmd, arg1, arg2], stdout=PIPE).communicate()[0]

        arg1 = "mousemove"
        arg2 = str(self.screen_width/2)
        arg3 = str(self.screen_height/2)
        win_id = Popen([cmd, arg1, arg2, arg3], stdout=PIPE).communicate()[0]

        arg1 = "click"
        arg2 = "1"
        win_id = Popen([cmd, arg1, arg2], stdout=PIPE).communicate()[0]
        win_id = Popen([cmd, arg1, arg2], stdout=PIPE).communicate()[0]

        arg1 = "mousemove" 
        arg2 = "0"
        arg3 = "0"
        win_id = Popen([cmd, arg1, arg2, arg3], stdout=PIPE).communicate()[0]


handler = SkypeHandler()

parser = OptionParser()
parser.add_option("-a", "--answer",
                  action="callback", callback=handler.answer_call,
                  help="Answer all currently ringing calls")
parser.add_option("-e", "--hangup",
                  action="callback", callback=handler.end_call,
                  help="End all active calls")
parser.add_option("-m", "--maximise",
                  action="callback", callback=handler.maximise_video,
                  help="End all active calls")

(options, args) = parser.parse_args()

