
import kivy
import time

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.core.window import Window
from kivy.config import Config

from tkinter import filedialog
from tkinter import simpledialog
from KivyOnTop import register_topmost, unregister_topmost

from datetime import datetime


from tkinter import *

folder_selected = filedialog.askopenfilename()

print(folder_selected)

Court_name = simpledialog.askstring(title="Court Name",prompt="What Court is this?\t\t\t")

print(Court_name)
#Builder.load_file('update_label.kv')


filePath = (folder_selected)

print(filePath)

root = Tk()
root.withdraw()



#f = open(r"C:\Users\William\Documents\University\BeepDetails.log", "r")

Builder.load_string("""
<MySec>:
    rgba: (1,.2,.2,.2)
    orientation: 'vertical'

    BoxLayout:
        size_hint: (1, .1)
        canvas.before:
            Color:
                rgba: root.rgba
            Rectangle:
                size: self.size
                pos: self.pos

        Label:
            id: Court_name
            text: root.Court_name
            font_size: 30
            size_hint: (1, 1)
            bold: True

    BoxLayout:
        size_hint: (1, .05)
        canvas.before:
            Color:
                rgba: root.rgba
            Rectangle:
                size: self.size
                pos: self.pos

        Label:
            id: Positive_text
            text: 'Far'
            font_size: 15
            size_hint: (1, 1)
    

    BoxLayout:
        size_hint: (1, .35)
        canvas.before:
            Color:
                rgba: root.rgba
            Rectangle:
                size: self.size
                pos: self.pos

        Label:
            id: Positive
            text: root.Positive
            font_size: 80
            size_hint: (1, 1)

    BoxLayout:
        size_hint: (1, .05)
        canvas.before:
            Color:
                rgba: root.rgba
            Rectangle:
                size: self.size
                pos: self.pos

        Label:
            id: Negative_text
            text: 'Near'
            font_size: 15
            size_hint: (1, 1)

    BoxLayout:
        size_hint: (1, .35)
        canvas.before:
            Color:
                rgba: root.rgba
            Rectangle:
                size: self.size
                pos: self.pos

        Label:
            id: Negative
            text: root.Negative
            font_size: 80
            size_hint: (1, 1)



    BoxLayout:
        size_hint: (1, .1)
        canvas.before:
            Color:
                rgba: root.rgba
            Rectangle:
                size: self.size
                pos: self.pos

        Label:
            id: info
            text: root.info
            font_size: 30
            size_hint: (1, 1)
            bold: True
""")
Window.size = (300, 300)


def read_5th_line(filename,num):
    with open(filename, 'r') as file:
        lines = file.readlines()
        if len(lines) >= 5:
            fifth_line = lines[num].strip()  # Index 4 represents the 5th line (0-based indexing)
            return fifth_line
        else:
            return None



class MySec(BoxLayout):
    Positive = StringProperty('')
    info = StringProperty('')
    Negative = StringProperty('')
    Court_name = StringProperty('')
    Config.set('kivy','window_icon','./Sunny.ico')
    Config.set('graphics', 'always_on_top','1')
    #Config.set('graphics','position', 'custom' )
    Config.set('graphics','left',10)
    Config.set('graphics','top',50)
    Config.set('graphics','height',300)
    Config.set('graphics','width',300)
    Config.write()





class MyApp(App):
    def build(self):
        self.title = 'Sunny V1.0'



        #register_topmost(Window, 'Sunny V1.0')
        Clock.schedule_interval(lambda dt: self.update_time(), 5)
        
        return MySec()

    def update_time(self):

            self.root.Court_name = Court_name
            with open(filePath,'r') as f:
                 line_5 = read_5th_line(filePath,4)
                 print(datetime.now())

                 if line_5 == '"sunStrengthLinkBoth" : true,':
                         print('Sun Strength Linked')
                         self.root.info = 'Linked'
                 elif line_5 == '"sunStrengthLinkBoth" : false,':
                         print('Sun Strength NOT Linked')
                         self.root.info = 'NOT Linked'
                 else:
                         print("Error: Cant see if Sun Strength is Linked")
                         self.root.info = 'Error'
                 line_6 = read_5th_line(filePath,5)
                 XNeg = line_6.find('sunStrengthNegX')
                 XNeg = float(line_6[XNeg+19:XNeg+22])
                 print(XNeg)

                 line_7 = read_5th_line(filePath,6)
                 XPos = line_7.find('sunStrengthPosX')
                 XPos = float(line_7[XPos+19:XPos+22])
                 print(XPos)
                 print('\n')
                 Average = (XNeg + XPos)/2
                 Average = round(Average,1)



                 if Average == 0.0:
                        XNeg = str(XNeg)
                        XPos = str(XPos)
                        self.root.Negative = XNeg
                        self.root.Positive = XPos
                        self.root.rgba = [0/255, 10/255, 10/255, 1.0] 

                 elif Average <= 0.1:
                        XNeg = str(XNeg)
                        XPos = str(XPos)
                        self.root.Negative = XNeg
                        self.root.Positive = XPos
                        self.root.rgba = [0/255, 58/255, 71/255, 1.0]    
                               
                                   

                 elif ((Average > 0.1) and (Average <= 0.3)):
                        XNeg = str(XNeg)
                        XPos = str(XPos)
                        self.root.Negative = XNeg
                        self.root.Positive = XPos
                        self.root.rgba = [0/255, 79/255, 97/255, 1.0] 

                       

                 elif ((Average > 0.3) and (Average <= 0.6)):
                        XNeg = str(XNeg)
                        XPos = str(XPos)
                        self.root.Negative = XNeg
                        self.root.Positive = XPos
                        self.root.rgba = [0/255, 117/255, 143/255, 1.0] 

                                    

                 elif ((Average > 0.6) and (Average <= 0.9)):
                        XNeg = str(XNeg)
                        XPos = str(XPos)
                        self.root.Negative = XNeg
                        self.root.Positive = XPos
                        self.root.rgba = [0/255, 157/255, 192/255, 1.0] 


                                    

                 elif Average > 0.9:
                        XNeg = str(XNeg)
                        XPos = str(XPos)
                        self.root.Negative = XNeg
                        self.root.Positive = XPos
                        self.root.rgba = [0/255, 208/255, 255/255, 1.0] 

                                    

             

        
                    

if __name__ == '__main__':
    MyApp().run()













                






        



