from kivy.core.window import Window
from kivy.lang import Builder

from kivy.uix.screenmanager import Screen, ScreenManager

try:
    from widgets import screen_helper
except:
    pass #ModuleNotFoundError: No module named 'widgets'

from kivymd.app import MDApp
from kivymd.uix.filemanager import MDFileManager
from kivymd.toast import toast
from kivymd.utils.fitimage import FitImage
from kivy.properties import (
    StringProperty,
    BooleanProperty,
    ObjectProperty,
    NumericProperty,
    ListProperty,
    OptionProperty,
)

class Studio(Screen):
    #this is a good idea, to implement it, you should add it using clock

    pass



KV = '''
Screen:
    ScreenManager:
        id: manager #this is needed
        Screen:
            name : 'studio'
            MDToolbar:
                pos_hint: {'top': 1}
                title: "DISEASES DETECTION"
                left_action_items: [['arrow-left', lambda x: app.goto("studio")]]
                #md_bg_color: [1,0,1,.3] #if you need a fully transparent Toolbar use [0,0,0,0]
                
            MDFloatLayout:
                Image: #the FitImage: class does not update media, but Image does
                    id: Diseases_Leaf_pic
                    size_hint: 0.40, 0.40
                    source: "Photos/ear.JPG"
                    pos_hint: {"center_x": .500, "center_y": .7000}
                    radius: 60, 60, 60, 60
                    #
                TooltipMDIconButton:  #kivy.factory.FactoryException: Unknown class <TooltipMDIconButton>
                    tooltip_text : 'Upload Image'
                    id: edit_Diseases_Leaf_pic
                    icon: "image-edit-outline"
                    disabled: True
                    pos_hint: {'center_x':0.500,'center_y':0.55}
                    user_font_size: "25sp"
                    on_release: app.file_manager_open_for_Diseases_Leaf()
                    
                MDIconButton:
                    icon: "home"
                    theme_text_color: "Custom"
                    text_color: app.theme_cls.primary_color
                    pos_hint: {"center_x": 0.1}
                    user_font_size: "35sp"
                    on_release:app.goto("navigator3") #root.ids.manager.current = 'navigator3'
              
                MDIconButton:
                    icon: "artstation"
                    theme_text_color: "Custom"
                    text_color: app.theme_cls.primary_color
                    pos_hint: {"center_x": 0.1}
                    pos_hint: {"center_x": 0.50}
                    user_font_size: "35sp"
                    on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'                  
                                
                MDIconButton:
                    icon: "book-open-page-variant"
                    theme_text_color: "Custom"
                    text_color: app.theme_cls.primary_color
                    pos_hint: {"center_x": 0.88}
                    user_font_size: "35sp"
                    on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'


                MDRaisedButton:
                    text: "DETECT DISEASES"
                    pos_hint: {"center_x": 0.5, "center_y":0.30}
                    size_hint: .7,0.05
                    on_release:app.btnb()                 

 


                MDLabel:
                    text: ""
                    halign: "center"
                    pos_hint: {"center_y":.25}
                    font_style: "Subtitle2"
                    id: l9
                    color: 1,0,1,1
                    
                MDLabel:
                    text: ""
                    halign: "center"
                    pos_hint: {"center_y":.20}
                    font_style: "Subtitle2"
                    id: l15
                    color: 1,0,1,1
                                  
                MDLabel:
                    text: ""
                    halign: "center"
                    pos_hint: {"center_y":.20}
                    font_style: "Subtitle2"
                    id: l1
                    color: 1,0,1,1
                    
                MDLabel:
                    text: ""
                    halign: "center"
                    pos_hint: {"center_y":4.0}
                    font_style: "Subtitle2"
                    id: l10
                    color: 1,0,1,1
                    
                MDLabel:
                    text: "Please Select"
                    halign: "center"
                    pos_hint: {"center_y":.85}
                    font_style: "Subtitle2"
                    id: l1
                    color: 1,0,1,1                    
                    
                FloatLayout:
                    BoxLayout:
                        pos_hint: {'center_x': -.5,'center_y':0.2}
                        MDIconButton:
                            icon: 'arrow-left'
                            pos_hint: {'center_x':0.03,'center_y':0.97}
                            user_font_size: "25sp"
                            theme_text_color : 'Custom'
                            text_color : 0,0,0,1
                            on_release : 
                                app.goto("navigator") #root.ids.manager.current = 'navigator' #you should have another screen called navigator
                                #root.ids.manager.transition.direction = 'right'
                                print(1111)
                                
                        MDIconButton:
                            icon: 'home'
                            pos_hint: {'center_x':0.1,'center_y':0.97}
                            user_font_size: "25sp"
                            theme_text_color : 'Custom'
                            text_color : 0,0,0,1
                            on_release :
                                app.goto("home") 
                                #root.manager.current = 'home' #you should have another screen called home
                                #this does not work root.manager.transition.direction = 'right'
                                print(2222)
                FloatLayout:
                    BoxLayout:
                        pos_hint: {'center_x':1.2,'center_y':0.3}
                        TooltipMDIconButton: #kivy.factory.FactoryException: Unknown class <TooltipMDIconButton>
                            id: edit_cover_pic


<TooltipMDIconButton@MDIconButton+MDTooltip>
<ScreenB>:
    name: "home"
    MDToolbar:
        pos_hint: {'top': 1}
        title: "Home Screen"
        left_action_items: [['arrow-left', lambda x: app.goto("studio")]]
        #md_bg_color: [1,0,1,.3] #if you need a fully transparent Toolbar use [0,0,0,0]
        
    MDIconButton:
        icon: "atom-variant"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.1}
        user_font_size: "35sp"
        on_release:app.goto("navigator3") #root.ids.manager.current = 'navigator3'
              
    MDIconButton:
        icon: "artstation"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.1}
        pos_hint: {"center_x": 0.50}
        user_font_size: "35sp"
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'                  
                                
    MDIconButton:
        icon: "book-open-page-variant"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.88}
        user_font_size: "35sp"
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'
        
        
        
<ScreenC>:
    name: "navigator3"
    MDToolbar:
        pos_hint: {'top': 1}
        title: "Navigator Screen"
        left_action_items: [['arrow-left', lambda x: app.goto("studio")]]
        #md_bg_color: [1,0,1,.3] #if you need a fully transparent Toolbar use [0,0,0,0]

    MDLabel:
        text: "1st Screen"
        halign: "center"
        pos_hint: {"center_y":.65}
        font_style: "Subtitle2"
        
        
    MDIconButton:
        icon: "atom-variant"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.1}
        user_font_size: "35sp"
        on_release:app.goto("navigator3") #root.ids.manager.current = 'navigator3'
              
    MDIconButton:
        icon: "artstation"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.1}
        pos_hint: {"center_x": 0.50}
        user_font_size: "35sp"
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'                  
                                
    MDIconButton:
        icon: "book-open-page-variant"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.88}
        user_font_size: "35sp"
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'
        
        

<ScreenD>:
    name: "navigator"
    MDToolbar:
        pos_hint: {'top': 1}
        title: "Recommdation"
        left_action_items: [['arrow-left', lambda x: app.goto("studio")]]
        #md_bg_color: [1,0,1,.3] #if you need a fully transparent Toolbar use [0,0,0,0]

    MDLabel:
        text: "Recommndation 1 "
        halign: "center"
        pos_hint: {"center_y":.65}
        font_style: "Subtitle2"
        
    MDLabel:
        text: "Recommndation 2 "
        halign: "center"
        pos_hint: {"center_y":.50}
        font_style: "Subtitle2"
        
    MDLabel:
        text: "Recommndation 3 "
        halign: "center"
        pos_hint: {"center_y":.40}
        font_style: "Subtitle2"
        
        
    MDLabel:
        text: "Sample Msg"
        halign: "center"
        pos_hint: {"center_y":.25}
        font_style: "Subtitle2"
        id: l2
        color: 1,0,1,1
 
    MDIconButton:
        icon: "atom-variant"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.1}
        user_font_size: "35sp"
        on_release:app.goto("navigator3") #root.ids.manager.current = 'navigator3'
              
    MDIconButton:
        icon: "artstation"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.1}
        pos_hint: {"center_x": 0.50}
        user_font_size: "35sp"
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'                  
                                
    MDIconButton:
        icon: "book-open-page-variant"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        pos_hint: {"center_x": 0.88}
        user_font_size: "35sp"
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'
        on_release:app.goto("navigator") #root.ids.manager.current = 'navigator'
 
 
    MDRaisedButton:
        text: "sss"
        pos_hint: {"center_x": 0.5, "center_y":0.30}
        size_hint: .7,0.05
        on_release:
            root.btnTest()  
 
'''

from kivymd.uix.button import MDIconButton
from kivymd.uix.tooltip import MDTooltip
class TooltipMDIconButton(MDIconButton, MDTooltip):

    pass


class ScreenB(Screen):

    pass

class ScreenC(Screen):
    pass

class ScreenD(Screen):
    def btnTest(self):
        self.ids.l2.text = 'Recived Message'
    pass

class Diseases_LeafPicture(FitImage):
    newpic=StringProperty()
#jbsidis
import os
from kivy.clock import Clock




class Example(MDApp):



    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        Window.bind(on_keyboard=self.events)
        self.manager_open = False

        self.image_is_Diseases_Leaf_or_cover="none"



    def goto(self,name_of_the_screen):
        self.root.ids.manager.current = name_of_the_screen #'navigator'

    def nn(self):
        self.file_manager = MDFileManager()
        self.file_manager.exit_manager=self.exit_manager
        self.file_manager.select_path=self.select_path



## This error is bacause in your own example, the ID of the widgets are different or they must
## be accessed in a different way
##        self.root.ids.n1.disabled=False
##   File "kivy/properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
## AttributeError: 'super' object has no attribute '__getattr__'
        ##the id "n1" doesnot exists in the kivy Lang section, so we should specify which one is it
        ##the id "edit_Diseases_Leaf_pic" does exists in the kivy Lang section
        #BAD ===== self.root.ids.n1.disabled=False
        #GOOD
        self.root.ids.edit_Diseases_Leaf_pic.disabled=False
        self.root.ids.edit_cover_pic.disabled=False
        #preview=False
        #if you will be using a lot of screens add all of them by doing this
        #This must be executed once only, at the beginning
        self.root.ids.manager.add_widget(ScreenB())
        self.root.ids.manager.add_widget(ScreenC())
        self.root.ids.manager.add_widget(ScreenD())

    def build(self):
        image_is_Diseases_Leaf_or_cover="none"
        Clock.schedule_once(lambda x: self.nn(),3)
        return Builder.load_string(KV)
    def chpic(self,new):
        if os.path.isfile(new)==True:
            if self.image_is_Diseases_Leaf_or_cover=="cover":
                self.root.ids.cover_pic.source=new
                print("The picture on 'id: cover_pic' was changed to:",self.root.ids.cover_pic.source)


            if self.image_is_Diseases_Leaf_or_cover=="Diseases_Leaf":
                self.root.ids.Diseases_Leaf_pic.source=new
                print("The picture on 'id: Diseases_Leaf_pic' was changed to:",self.root.ids.Diseases_Leaf_pic.source)
                # self.root.ids.l1.text = self.root.ids.Diseases_Leaf_pic.source



    def file_manager_open_for_Diseases_Leaf(self):
        self.image_is_Diseases_Leaf_or_cover="Diseases_Leaf"
        self.file_manager.show('/')  # output manager to the screen
        self.manager_open = True

    def file_manager_open_for_cover(self):
        self.image_is_Diseases_Leaf_or_cover="cover"
        self.file_manager.show('/')  # output manager to the screen
        self.manager_open = True

    def select_path(self, path):

        self.exit_manager()
     #   print("here is path")
      #  print(path)
        self.root.ids.l10.text = path



        if os.path.isfile(path)==True:
            Clock.schedule_once(lambda x:self.chpic(path),1)
        #toast(path) #here the location for the image file will be returned
     #   self.root.ids.l1.text = 'Test Positive'
        return path


    def exit_manager(self, *args):
        self.manager_open = False
        self.file_manager.close()

    def events(self, instance, keyboard, keycode, text, modifiers):
        if keyboard in (1001, 27):
            if self.manager_open:
                self.file_manager.back()
        return True

    def btnb(self):



        import warnings
        warnings.filterwarnings('ignore', category=FutureWarning)
        import tensorflow as tf
        classifierLoad = tf.keras.models.load_model('Paddy_Diseases_Leaf_Brain/Paddy_Diseases.h5')

        import numpy as np
        from keras.preprocessing import image

        test_image = image.load_img(self.root.ids.l10.text, target_size=(200, 200))

        test_image = image.img_to_array(test_image)

     #   print(test_image)

        test_image = np.expand_dims(test_image, axis=0)

        result = classifierLoad.predict(test_image)

        # print(result[0])
        print("------------------ Predicted  ------------------")

        # print(result)

        ###################################################

        print(result)

        if result[0][0] == 1:
            print("Bacterialblight")
            self.root.ids.l9.text = 'Bacterialblight'
        elif result[0][1] == 1:
            print("Blast")
            self.root.ids.l9.text = 'Blast'
        elif result[0][2] == 1:
            print("Brownspot")
            self.root.ids.l9.text = 'Brownspot'
        elif result[0][3] == 1:
            print("Tungro")
            self.root.ids.l9.text = 'Tungro'
            self.root.ids.l15.text = 'Abc manunthu , xyz adadasd , asdas'











    pass




Example().run()