#!/usr/bin/env python3
from gi.repository import Gtk, Gdk, Pango
from threading import Thread
import time
import subprocess
import sys

"""
Take a Break
Copyright (C) 2015  Jacob Vlijm
contact: vlijm@planet.nl
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 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/>.
"""

class Splash(Gtk.Window):

    def __init__(self):
        
        Gtk.Window.__init__(self, title="Take a Break countdown")
        maingrid = Gtk.Grid()
        maingrid.set_column_spacing(40)
        self.add(maingrid)
        self.duration = int(sys.argv[1])

        image = Gtk.Image()
        image.set_from_file("/usr/share/takeabreak/icon/break_64.png")
        maingrid.attach(image, 0, 0, 1, 1)
        maingrid.set_border_width(40)

        self.label = Gtk.Label("Time to take a break!")
        self.label.set_width_chars(18)
        self.label.modify_font(Pango.FontDescription('Ubuntu 30'))
        maingrid.attach(self.label, 1, 0, 1, 1)

        self.timelabel = Gtk.Label("     ")
        self.timelabel.modify_font(Pango.FontDescription('Ubuntu 30'))
        self.timelabel.set_width_chars(7)
        self.timelabel.modify_fg(
            Gtk.StateFlags.NORMAL, Gdk.color_parse("green")
            )
        maingrid.attach(self.timelabel, 2, 0, 1, 1)
        
        Thread(target = self.timer).start()
        
    def timer(self):
        duration = self.duration
        while True:
            time.sleep(1)
            duration = duration - 1
            self.timelabel.set_text(str(duration))
            if duration % 5 == 0:
                try:
                    subprocess.Popen(["wmctrl", "-a", "Take a Break countdown"])
                except subprocess.CalledProcessError:
                    pass
            if duration <= 0:
                self.label.set_text("Back to work...")
                try:
                    subprocess.Popen(["wmctrl", "-a", "Take a Break countdown"])
                    w_list = subprocess.check_output(
                        ["wmctrl", "-lp"]
                        ).decode("utf-8")
                    splashpid = [l.split()[2] for l in w_list.splitlines()\
                                 if "Take a Break countdown" in l][0]
                    time.sleep(2)
                    subprocess.Popen(["kill", splashpid])
                except (IndexError, subprocess.CalledProcessError):
                    pass
                break

def splashwindow():
    window = Splash()
    window.set_decorated(False)
    window.set_resizable(False)
    window.override_background_color(
        Gtk.StateType.NORMAL, Gdk.RGBA(0,0,0,1)
        )
    window.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse("grey"))
    window.set_opacity(0.8)
    window.set_position(Gtk.WindowPosition.CENTER)
    window.set_icon_from_file("/usr/share/takeabreak/icon/break_64.png")  # path
    window.show_all()
    Gtk.main()

splashwindow()












        
