#!/usr/bin/python3

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf

import subprocess
from subprocess import Popen, PIPE

import os
import sys
import glob

SELFDIR=os.path.dirname(os.path.realpath(__file__))+"/"

def cmdexec(cmd1="",cmd2=""):
  global win
  cmd=cmd1
  if cmd2!="": cmd=cmd2
  cmd=cmd.strip()
  if cmd.endswith('&'):
    print(cmd)
    cmd=cmd[:-1]
    print(cmd)
    subprocess.Popen(cmd,shell=True)
    return
  else:
    a=subprocess.getoutput(cmd)
    return a

class HelpWindow(Gtk.Window):    
  def singlebutton(self,text,bild,kommando,kommando2):
    btn0 = Gtk.Button(xalign=0)
    btn0.connect("clicked", kommando, kommando2)
    btn0_grid=Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    btn0_label=Gtk.Label(label=text,xalign=0.5,justify=2);
    btn0_img=Gtk.Image()
    btn0_img.set_from_pixbuf(GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=SELFDIR+bild,width=-1, height=60, preserve_aspect_ratio=True))
    btn0_grid.attach(btn0_img, 0, 0, 1, 1)
    btn0_grid.attach(btn0_label, 0, 1, 1, 1)
    btn0.add(btn0_grid)
    return btn0
    
  def __init__(self):
    global SELFDIR
    super().__init__(title="XLinux Hilfecenter")
    if os.path.isfile(SELFDIR+"help.png"):
      self.set_icon_from_file(SELFDIR+"help.png")
    self.set_position(Gtk.WindowPosition.CENTER)
    grid = Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    #grid.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(.5,.5,.5,.5))
    symbolsize=60


    
    # Kurzeinweisung
    btn0=self.singlebutton("Kurz-Einführung \n", "kurzeinweisung.svg", self.wincmd, "/opt/XLinux/Hilfe/Einweisung/bildergalerie.py &")
    grid.attach(btn0, 0, 1, 1, 1)
    if not os.path.isfile("/opt/XLinux/Hilfe/Einweisung/bildergalerie.py"):
      btn0.set_sensitive(False)

    #WLAN-Verbinden
    btn1=self.singlebutton("Anleitung zum \nWLAN verbinden ", "wifi.svg", self.wincmd, "exo-open '/opt/XLinux/Hilfe/WLAN verbinden.pdf' &")
    grid.attach(btn1, 1, 1, 1, 1)

    # Handbuch
    btn4=self.singlebutton("Handbuch (PDF) \n", "handbuch.svg", self.wincmd, "exo-open /opt/XLinux/Hilfe/Handbuch_*.pdf &")
    grid.attach(btn4, 2, 1, 1, 1)
    if len(glob.glob("/opt/XLinux/Hilfe/Handbuch_*.pdf"))==0:
      btn4.set_sensitive(False)

    # Hilfe & Anleitungen (Online)
    btn2=self.singlebutton("Hilfe & Anleitungen \n", "onlinehilfe.svg", self.wincmd_online, "exo-open https://support.mastro-it.de/service?uuid="+cmdexec("xlinux-systemid")+" &")
    grid.attach(btn2, 0, 2, 1, 1)
   

    # Fernwartung   
    btn3=self.singlebutton("kostenpflichtige Hilfe \nFernwartung", "fernwartung.svg", self.open_remote_service, "" )
    grid.attach(btn3, 1, 2, 1, 1)
      
    # Softwarecenter
    btn5=self.singlebutton("Anwendungen & Spiele \n", "softwarecenter.svg", self.wincmd_online, "/opt/XLinux/softwarecenter/Softwarecenter.py &" )
    grid.attach(btn5, 2, 2, 1, 1)
      

    self.add(grid);
    

  def wincmd(self,button,befehl):
    cmdexec(befehl)
    Gtk.main_quit()
    
  def wincmd_online(self,button,befehl):
    if checkonline():
      cmdexec(befehl)
      Gtk.main_quit()
    else:
      dialog=Gtk.MessageDialog(transient_for=self,flags=0,message_type=Gtk.MessageType.INFO,buttons=Gtk.ButtonsType.OK,text="Funktion nicht verfügbar")
      dialog.format_secondary_text("Diese Funktion ist nur verfügbar, wenn Sie mit dem Internet verbunden sind.")
      dialog.run()
      dialog.destroy()
      self.show()

  def open_remote_service(self,button,*_):
    if checkonline():
      self.hide()
      win=RemoteSupportWindow()
      win.connect("destroy", win.open_main)
      win.show_all()
    else:
      dialog=Gtk.MessageDialog(transient_for=self,flags=0,message_type=Gtk.MessageType.INFO,buttons=Gtk.ButtonsType.OK,text="Fernwartung nicht verfügbar.")
      dialog.format_secondary_text("Stellen Sie bitte die Internetverbindung her. \n \nFalls Sie weitere Hilfe benötigen, kontaktieren Sie uns bitte telefonisch.")
      dialog.run()
      dialog.destroy()
      self.show()

class RemoteSupportWindow(Gtk.Window):    
  def __init__(self):
    global SELFDIR,ONLINE
    super().__init__(title="XLinux Hilfecenter")
    if os.path.isfile(SELFDIR+"help.png"):
      self.set_icon_from_file(SELFDIR+"help.png")
    self.set_position(Gtk.WindowPosition.CENTER)
    grid = Gtk.Grid(margin=10,column_spacing=20,row_spacing=20,hexpand=True,column_homogeneous=True,row_homogeneous=False)

    label=Gtk.Label(xalign=0)
    label.set_markup("<b>Telefonische Hilfe / Fernwartung</b>\nWenn Sie individuelle Fragen haben, können wir Ihnen persönlich helfen.\nDiese Dienstleistung wird Ihnen nach Zeitaufwand in Rechnung gestellt.\nMehr Informationen über unsere Webseite: <a href='https://www.mastro-it.de'>www.mastro-it.de</a>\n\n<b>Die nächsten Schritte:</b>")
    grid.attach(label, 0, 0, 3, 1)
    btn3_img=Gtk.Image()
    btn3_img.set_from_pixbuf(GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=SELFDIR+"fernwartung.svg",width=100, height=100, preserve_aspect_ratio=True))
    grid.attach(btn3_img, 3, 0, 1, 1)
    
    label_schritt1=Gtk.Label(xalign=0)
    label_schritt1.set_markup("<b>1.</b> Termin für telefonische Unterstützung buchen: <a href='https://www.mastro-it.de/rueckruf'>Termin buchen</a>")
    grid.attach(label_schritt1, 0, 1, 4, 1)

    label_schritt2=Gtk.Label(xalign=0)
    label_schritt2.set_markup("<b>2.</b> Zum Termin müssen Sie telefonisch erreichbar sein und der Computer muss mit dem Internet verbunden sein.")
    grid.attach(label_schritt2, 0, 2, 4, 1)

    label_schritt3=Gtk.Label(xalign=0)
    label_schritt3.set_markup("<b>3.</b> Wir rufen Sie an und werden Sie bei Bedarf auffordern, den 'Fernwartung starten'-Button zu drücken.")
    grid.attach(label_schritt3, 0, 3, 4, 1)

    btn3 = Gtk.Button(label='Fernwartung starten',xalign=0.5)
    btn3.connect("clicked", self.wincmd, "/opt/XLinux-Fernwartung/2022_fernwartung.sh &")
    grid.attach(btn3, 0, 4, 1, 1)

    btn3 = Gtk.Button(label='Zurück',xalign=0.5)
    btn3.connect("clicked", self.open_main)
    grid.attach(btn3, 3, 4, 1, 1)
    
    self.add(grid);

  def wincmd(self,button,befehl):
    cmdexec(befehl)
    Gtk.main_quit()

  def open_main(self,button):
    global win
    win.show_all()
    self.destroy()
    


def checkonline():
  ONLINE=False
  if cmdexec("xlinux-checkonline").strip()=="ja":
    ONLINE=True
  return ONLINE

win=HelpWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
