'''
Author: <NAME>
Date: May 20th, 2019
'''

import wx
import os
import shutil
import webbrowser
from wxGUI.CreditsGUI import CreditsGUI
from PIL import Image


class SettingsGUI(wx.Frame):

    def __init__(self):

        # setup window
        wx.Frame.__init__(self, None, wx.ID_ANY, "Control Panel Settings",
                          style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)

        # create layout
        self.panel = SettingsPanel(self)
        self.SetSize(0, 0, 480, 140)
        self.Centre()
        self.Layout()
        self.Show()

        # check if it's going to close
        self.instance = wx.SingleInstanceChecker("SingleApp-%s" % wx.GetUserId())
        self.Bind(wx.EVT_CLOSE, self.frame_on_close)

    def frame_on_close(self, event):
        self.panel.cgui.Hide()
        self.Hide()


class SettingsPanel(wx.Panel):

    def __init__(self, frame):

        wx.Panel.__init__(self, frame)

        # create credits
        self.cgui = CreditsGUI()
        self.cgui.Hide()

        # setup vertical box
        self.vbox = wx.BoxSizer(wx.VERTICAL)

        # create panels
        self.ip_row = self._ip_sizer()
        self.buttons_box = self._button_sizer()

        # create layout
        self.vbox.AddSpacer(20)
        self.vbox.Add(self.ip_row)
        self.vbox.AddSpacer(20)
        self.vbox.Add(self.buttons_box)

        # size boxes to fix
        self.SetSizer(self.vbox)
        self.Fit()

    def get_credits(self, frame):

        # place at front
        self.cgui.Raise()

        # open new window
        self.cgui.Show()

    def _ip_sizer(self):

        # create ip box components
        ip_row = wx.BoxSizer(wx.HORIZONTAL)
        lbl_ip = wx.StaticText(self, label="Robot\'s IP Address: ")
        self.ip_box = wx.TextCtrl(self, size=(200, 25))

        # add to box
        ip_row.Add((10, 10), proportion=1)
        ip_row.Add(lbl_ip)
        ip_row.Add((10, 10), proportion=1)
        ip_row.Add(self.ip_box)

        return ip_row

    # open website
    def go_to_website(self, frame):
        webbrowser.open('https://github.com/jangarong/GRR-Pi')

    def _button_sizer(self):

        # create buttons
        btn_manual = wx.Button(self, -1, "Repository\'s Website")
        btn_credits = wx.Button(self, -1, "Credits")
        btn_clear = wx.Button(self, -1, "Clear")

        btn_clear.Bind(wx.EVT_BUTTON, self.clear_files)
        btn_credits.Bind(wx.EVT_BUTTON, self.get_credits)
        btn_manual.Bind(wx.EVT_BUTTON, self.go_to_website)

        # add to panel
        buttons_row = wx.BoxSizer(wx.HORIZONTAL)
        buttons_row.Add((30, 30), proportion=1)
        buttons_row.Add(btn_manual)
        buttons_row.Add((30, 30), proportion=1)
        buttons_row.Add(btn_credits)
        buttons_row.Add((30, 30), proportion=1)
        buttons_row.Add(btn_clear)

        return buttons_row

    # create new placeholder
    def placeholder_asset(self, path):
        img = Image.new('RGB', (640, 480), (0, 0, 0))
        img.save(path, "PNG")

    def clear_files(self, frame, path='temp/'):

        # unlink files in folder
        for file in os.listdir(path):
            file_path = os.path.join(path, file)  # get file path

            # remove file
            if os.path.isfile(file_path):
                os.unlink(file_path)

            # remove directory
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)

        # create placeholder
        self.placeholder_asset('temp/placeholder.png')


'''
Author: <NAME>
Date: May 20th, 2019
'''

import wx
import os
import shutil
import webbrowser
from wxGUI.CreditsGUI import CreditsGUI
from PIL import Image


class SettingsGUI(wx.Frame):

    def __init__(self):

        # setup window
        wx.Frame.__init__(self, None, wx.ID_ANY, "Control Panel Settings",
                          style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)

        # create layout
        self.panel = SettingsPanel(self)
        self.SetSize(0, 0, 480, 140)
        self.Centre()
        self.Layout()
        self.Show()

        # check if it's going to close
        self.instance = wx.SingleInstanceChecker("SingleApp-%s" % wx.GetUserId())
        self.Bind(wx.EVT_CLOSE, self.frame_on_close)

    def frame_on_close(self, event):
        self.panel.cgui.Hide()
        self.Hide()


class SettingsPanel(wx.Panel):

    def __init__(self, frame):

        wx.Panel.__init__(self, frame)

        # create credits
        self.cgui = CreditsGUI()
        self.cgui.Hide()

        # setup vertical box
        self.vbox = wx.BoxSizer(wx.VERTICAL)

        # create panels
        self.ip_row = self._ip_sizer()
        self.buttons_box = self._button_sizer()

        # create layout
        self.vbox.AddSpacer(20)
        self.vbox.Add(self.ip_row)
        self.vbox.AddSpacer(20)
        self.vbox.Add(self.buttons_box)

        # size boxes to fix
        self.SetSizer(self.vbox)
        self.Fit()

    def get_credits(self, frame):

        # place at front
        self.cgui.Raise()

        # open new window
        self.cgui.Show()

    def _ip_sizer(self):

        # create ip box components
        ip_row = wx.BoxSizer(wx.HORIZONTAL)
        lbl_ip = wx.StaticText(self, label="Robot\'s IP Address: ")
        self.ip_box = wx.TextCtrl(self, size=(200, 25))

        # add to box
        ip_row.Add((10, 10), proportion=1)
        ip_row.Add(lbl_ip)
        ip_row.Add((10, 10), proportion=1)
        ip_row.Add(self.ip_box)

        return ip_row

    # open website
    def go_to_website(self, frame):
        webbrowser.open('https://github.com/jangarong/GRR-Pi')

    def _button_sizer(self):

        # create buttons
        btn_manual = wx.Button(self, -1, "Repository\'s Website")
        btn_credits = wx.Button(self, -1, "Credits")
        btn_clear = wx.Button(self, -1, "Clear")

        btn_clear.Bind(wx.EVT_BUTTON, self.clear_files)
        btn_credits.Bind(wx.EVT_BUTTON, self.get_credits)
        btn_manual.Bind(wx.EVT_BUTTON, self.go_to_website)

        # add to panel
        buttons_row = wx.BoxSizer(wx.HORIZONTAL)
        buttons_row.Add((30, 30), proportion=1)
        buttons_row.Add(btn_manual)
        buttons_row.Add((30, 30), proportion=1)
        buttons_row.Add(btn_credits)
        buttons_row.Add((30, 30), proportion=1)
        buttons_row.Add(btn_clear)

        return buttons_row

    # create new placeholder
    def placeholder_asset(self, path):
        img = Image.new('RGB', (640, 480), (0, 0, 0))
        img.save(path, "PNG")

    def clear_files(self, frame, path='temp/'):

        # unlink files in folder
        for file in os.listdir(path):
            file_path = os.path.join(path, file)  # get file path

            # remove file
            if os.path.isfile(file_path):
                os.unlink(file_path)

            # remove directory
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)

        # create placeholder
        self.placeholder_asset('temp/placeholder.png')


Author: <NAME> Date: May 20th, 2019 # setup window # create layout # check if it's going to close # create credits # setup vertical box # create panels # create layout # size boxes to fix # place at front # open new window # create ip box components # add to box # open website # create buttons # add to panel # create new placeholder # unlink files in folder # get file path # remove file # remove directory # create placeholder