#!/usr/bin/python3
# -*- coding: utf-8 -*-
import gi

gi.require_version("Gtk", "3.0")
gi.require_version("Vte", "2.91")
from gi.repository import Gtk, Vte, GLib


def on_child_exited(terminal, status):
    if status != 0:
        return
    dialog = Gtk.MessageDialog(
        transient_for=window,
        flags=0,
        message_type=Gtk.MessageType.QUESTION,
        buttons=Gtk.ButtonsType.YES_NO,
        text="Pardus ETAP 23 Kurulum Tamamlandı",
        secondary_text="Kurulum tamamlandı. Sistemi yeniden başlatmak istiyor musunuz?"
    )
    response = dialog.run()
    dialog.destroy()
    if response == Gtk.ResponseType.YES:
        GLib.spawn_command_line_sync("pkexec /sbin/reboot")
    Gtk.main_quit()


def start_vte_command():
    terminal.spawn_async(Vte.PtyFlags.DEFAULT,
                         "/", ["/bin/sh", "-c", "pkexec /lib/etap-installer/installer"], None,
                         GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None,
                         -1, None, None)


terminal = Vte.Terminal()
terminal.connect("child_exited", on_child_exited)
terminal.set_font_scale(2.0)

window = Gtk.Window()
window.add(terminal)
window.connect("destroy", Gtk.main_quit)

dialog = Gtk.MessageDialog(
    transient_for=window,
    flags=0,
    message_type=Gtk.MessageType.QUESTION,
    buttons=Gtk.ButtonsType.YES_NO,
    text="Pardus ETAP 23 Kurulum Onayı",
    secondary_text="<span color='red'><b>Diskinizdeki tüm veriler silinecektir. "
                   "Devam etmek istediğinize emin misiniz?</b></span>",
    secondary_use_markup=True
)
response = dialog.run()
dialog.destroy()

if response == Gtk.ResponseType.YES:
    start_vte_command()
    window.show_all()
    window.fullscreen()
else:
    GLib.idle_add(Gtk.main_quit)

Gtk.main()
