#!/usr/bin/env python3

import os
import json
import sqlite3
from pathlib import Path

import tkinter as tk
import tkinter.scrolledtext as tkst

from tkinter import ttk
from tkinter import font
from tkinter import Menu
from tkinter import IntVar
from tkinter import PhotoImage
from tkinter import messagebox
from tkinter import Checkbutton
from tkinter.filedialog import askdirectory
from tkinter.filedialog import askopenfilename

import csv_handler
import database_handler
import facesheet


class RestorativeJusticeApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        with open('app_files/defaults.json', 'r') as jsonFile:
            self.defaults = json.load(jsonFile)

        self.wm_title('Restorative Justice App -- Produced by Scott Frasier')
        img = PhotoImage(file='app_files/icon.gif')
        self.tk.call('wm', 'iconphoto', self._w, img)

        container = tk.Frame(self)
        container.pack(side='top', fill='both', expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = dict()
        self.frames['ButtonFrame'] = ButtonFrame(parent=container, controller=self)
        self.frames['OutputFrame'] = OutputFrame(parent=container, controller=self)
        self.frames['SelectionFrame'] = SelectionFrame(parent=container, controller=self)

        self.frames['ButtonFrame'].grid(row=1, column=0, sticky='nsew')
        self.frames['OutputFrame'].grid(row=0, column=0, sticky='nsew')
        self.frames['SelectionFrame'].grid(row=0, column=1, rowspan=3, sticky='nsew')

        self.frames['ButtonFrame'].config()
        self.frames['OutputFrame'].config()
        self.frames['SelectionFrame'].config()

        menubar = MenuBar(self)
        self.config(menu=menubar, pady=10, padx=10)
        self.AppLogic = AppLogic(controller=self)

        db_path = Path('app_files/app_db.sqlite3')
        if db_path.is_file():
            self.db = sqlite3.connect('app_files/app_db.sqlite3')