'''
this file implements the CasePreview class
in a new window allows case objects to be viewed/edited
'''
from datetime import datetime
from itertools import count
from tkinter import Toplevel, StringVar, Tk, Text, messagebox, DISABLED
import tkinter.ttk as ttk
from random import randint

from tkcalendar import DateEntry

from case import Case
from db import (
    COD,
    CONTRIES,
    INCIDENT,
    INCIDENT,
    ORGANIZATION,
    POLICESTATION,
    STATES,
    valid_years,
    db
)


class CasePreview(Toplevel):
    def __init__(self, parent, case: Case, edit=False):
        '''
        :edit:      boolean flag to track if the form should be editable
        '''
        Toplevel.__init__(self, parent)

        self.geometry('1087x506')
        self.title('Review')

        self.case = case
        self.edit = edit

        row = count(0, 1)

        # first name
        r = next(row)
        ttk.Label(self, text='First Name').grid(row=r, column=0)
        ttk.Label(self, text=case['fname']).grid(row=r, column=1)
        # last name
        r = next(row)
        ttk.Label(self, text='Last name').grid(row=r, column=0)
        ttk.Label(self, text=case['lname']).grid(row=r, column=1)
        # incident date
        r = next(row)
        ttk.Label(self, text='Incident Date (m/d/y)').grid(row=r, column=0)
        date = case['date']
        y, m, d = date.year - 2000, date.month, date.day
        ttk.Label(self, text=f'{m}/{d}/{y}').grid(row=r, column=1)
        # incident type
        r = next(row)
        ttk.Label(self, text='Incident Type').grid(row=r, column=0)
        ttk.Label(self, text=case['incident']).grid(row=r, column=1)

        # other incident/cause of death
        if case['incident'] == 'other':
            r = next(row)
            ttk.Label(self,text='Incident Other').grid(row=r, column=0)
            ttk.Label(self, text=case['incident_other']).grid(row=r, column=1) 
        elif case['incident'] == 'death':
            r = next(row)
            ttk.Label(self, text='Cause of Death').grid(row=r, column=0)
            ttk.Label(self, text=case['cod']).grid(row=r, column=1)
        # party size
        r = next(row)
        ttk.Label(self, text='Party Size').grid(row=r, column=0)
        ttk.Label(self, text=case['party_size']).grid(row=r, column=1)
        # incident location
        r = next(row)
        ttk.Label(self, text='Incident Location').grid(row=r, column=0)
        ttk.Label(self, text=case['location']).grid(row=r,column=1)
        # water related?
        r = next(row)
        ttk.Label(self, text='Water Related?').grid(row=r, column=0)
        ttk.Label(self, text=str(case['water_related'])).grid(row=r, column=1)
        # referred by
        r = next(row)
        ttk.Label(self, text='Referred by').grid(row=r, column=0)
        ttk.Label(self, text=case['referred']).grid(row=r, column=1)
        # other referral
        if case['referred'] == 'other':
            r = next(row)
            ttk.Label(self, text='Referred Other').grid(row=r, column=0)
            ttk.Label(self, text=case['referred_other']).grid(row=r, column=1)
        # police station
        if case['police'] != '':
            r = next(row)
            ttk.Label(self, text='Police Station').grid(row=r, column=0)
            ttk.Label(self, text=case['police']).grid(row=r, column=1)
        # visitor type
        r = next(row)
        ttk.Label(self, text='Visitor Type').grid(row=r, column=0)
        ttk.Label(self, text=case['visitor_type']).grid(row=r, column=1)
        # country of origin
        r = next(row)
        ttk.Label(self, text='Country').grid(row=r, column=0)
        ttk.Label(self, text=case['country']).grid(row=r, column=1)
        # state of origin
        if case['country'] == 'United States':
            r = next(row)
            ttk.Label(self, text='State').grid(row=r, column=0)
            ttk.Label(self, text=case['state']).grid(row=r, column=1)
        # case notes
        r = next(row)
        ttk.Label(self, text='Notes').grid(row=r, column=0)
        self.notes = Text(self, height=10)
        self.notes.insert('1.0', case['notes'])
        self.notes.config(state=DISABLED)
        self.notes.grid(row=r, column=1)


        # Buttons
        r = next(row)
        ttk.Button(self, text='Submit', command=self.commit).grid(
            row=r, column=1)
        r = next(row)
        ttk.Button(self, text='Back', command=self.destroy).grid(
            row=r, column=1)

    def commit(self):
        '''
        take the case data and write it to the database
        '''

        self.case['country'] = self.case['country'].lower()
        db.insert(self.case)
        messagebox.showinfo(
            title='Status', message='Saved to database successfully')
        self.destroy()


if __name__ == '__main__':
    root = Tk()
    case = Case(
            'Jane',
            'Doe',
            datetime(2021, 11, 24),
            'death',
            '',
            'natural_cause',
            3,
            'down town hilo',
            False,
            'airline',
            '',
            '',
            'land',
            'United States',
            'Arizona',
            'notes\noates\ntokes') 
    home = CasePreview(root, case)
    # home.pack()
    root.mainloop()
