#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# CopyCat is free software: you can redistribute it and/or modify
# it under the terms of WTFP Public License This program is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import subprocess

class DialogWindow(Gtk.Dialog):
    def __init__(self, parent, width, height):
        # Add OK and Cancel buttons at the bottom
        super().__init__(title="CopyCat 2.0.3", transient_for=parent, flags=0)
        self.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                         Gtk.STOCK_OK, Gtk.ResponseType.OK)
        self.set_default_size(width, height)
        self.set_position(Gtk.WindowPosition.CENTER)

        # icon with fallback
        try:
            self.set_icon_from_file('/usr/share/icons/hicolor/48x48/apps/copycat.svg')
        except Exception:
            pass

        # notebook
        self.notebook = Gtk.Notebook()
        self.vbox.pack_start(self.notebook, True, True, 0)

        # helper function to create a standard tab
        def create_tab(text, description, icon_path="/usr/share/icons/hicolor/48x48/apps/copycat.svg", markup=False):
            page = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
            page.set_border_width(20)
            page.set_halign(Gtk.Align.CENTER)
            page.set_valign(Gtk.Align.CENTER)

            icon = Gtk.Image.new_from_file(icon_path)
            page.pack_start(icon, False, False, 0)

            label = Gtk.Label()
            if markup:
                label.set_markup(description)  # use Pango markup
            else:
                label.set_text(description)
            label.set_line_wrap(True)
            label.set_justify(Gtk.Justification.CENTER)
            label.set_halign(Gtk.Align.CENTER)
            page.pack_start(label, False, False, 0)

            self.notebook.append_page(page, Gtk.Label(label=text))

        # create tabs
        create_tab("Debian Format",
                   'Format Debian in Creating Persistence iso Image for a USB Device.')
        create_tab("PCLinuxOS Installer",
                   'Create multiple Live Persistence iso Images for a USB Device.')
        create_tab("Debian Installer",
                   'Create a Live Persistence Debian iso Image for a USB Device.')
        create_tab("Live Creator",
                   'Create a Live Bootable Non Persistence for a USB Device.')
        create_tab("Help Info",
                   'Information Help for Creating and Using Debian Persistence.')
        self.show_all()

    def run(self):
        response = super().run()
        if response == Gtk.ResponseType.CANCEL:
            return response

        # Run the script corresponding to the selected tab
        page = self.notebook.get_current_page()
        if page == 0:
            subprocess.Popen(['/usr/share/copycat/scripts/format'])
        elif page == 1:
            subprocess.Popen(['/usr/share/copycat/scripts/pclos'])
        elif page == 2:
            subprocess.Popen(['/usr/share/copycat/scripts/persistence'])
        elif page == 3:
            subprocess.Popen(['/usr/share/copycat/scripts/iso'])
        elif page == 4:
            subprocess.Popen(['/usr/share/copycat/scripts/help'])
        elif page == 5:
            subprocess.Popen(['/usr/share/copycat/scripts/help'])

        return response


if __name__ == "__main__":
    dialog = DialogWindow(None, 400, 100)
    response = dialog.run()

    if response == Gtk.ResponseType.OK:
        print("OK clicked")
    elif response == Gtk.ResponseType.CANCEL:
        print("Cancel clicked")

    dialog.destroy()

