from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.graphics.texture import Texture
from kivy.clock import Clock
from kivy.metrics import dp
from kivy.properties import StringProperty
import JsonUtl as jsonutil
import cv2
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.behaviors import ButtonBehavior


class ButtonImage(ButtonBehavior, Image):
    pass

class MainScreen(Screen):
    ServerListVar = jsonutil.OpenJsontoArray() 
    BtnId = 0
    DelBtnId = 0

    def __init__(self, **kw):
        super().__init__(**kw)
        for i in range (0,len(self.ServerListVar)):
            self.CreateButtons(i)

    def Clicked(self,widget):
        Adress = self.ids.StreamAdress
        if not Adress.text == "":
            self.ServerListVar.append(Adress.text.strip())
            jsonutil.SaveArraytoJson(self.ServerListVar)
            self.CreateButtons(len(self.ServerListVar)- 1)
            Adress.text = ""
    
    def CamSelectButtonPressed(self,x):
            app = App.get_running_app()
            app.data = self.ServerListVar[x]
            app.root.current = "Stream"  

    def CamDeleteButtonPressed(self,x):
        del self.ServerListVar[x]
        self.ids.CamSelectStackLayout.clear_widgets()
        jsonutil.SaveArraytoJson(self.ServerListVar)
        self.ServerListVar = jsonutil.OpenJsontoArray() 
        self.BtnId = 0
        self.DelBtnId = 0
        for i in range (0,len(self.ServerListVar)):
            self.CreateButtons(i)
    
    def CreateButtons(self, i):
        box = BoxLayout(size_hint = (None, None),width= dp(200), height = dp(200),orientation = "vertical")
        bimage = ButtonImage()
        #b2 = Button(size_hint = (None, None),width = dp(200), height = dp(50), background_color = (1,0,0,1), text = "Delete Stream")
        b2 = ButtonImage(size_hint = (None, None),width = dp(200), height = dp(50))
        b2.source = "DeleteButton.png"
        bimage.bind(on_press=lambda instance, button_id=self.BtnId: self.CamSelectButtonPressed(button_id))
        self.BtnId = self.BtnId + 1
        b2.bind(on_press=lambda instance, button_id=self.DelBtnId: self.CamDeleteButtonPressed(button_id))
        self.DelBtnId = self.DelBtnId + 1
        capture = cv2.VideoCapture(self.ServerListVar[i],cv2.CAP_FFMPEG, [cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY]) 
        print(capture.read())
        ret, frame = capture.read()
        if ret:
            buf = cv2.flip(frame, 0).tobytes()  
            texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
            texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
            bimage.texture = texture
        box.add_widget(bimage)
        box.add_widget(b2)
        self.ids.CamSelectStackLayout.add_widget(box)

class StreamView(Screen): 
    output = StringProperty() 
    event = None
    def on_enter(self):
        app = App.get_running_app()
        self.output = app.data
        self.capture = cv2.VideoCapture(self.output, cv2.CAP_FFMPEG, [cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY])  
        b = Button(size_hint = (0.05, 0.05), pos_hint = {"top":1}, background_color = (1,0,0,1))
        b = ButtonImage(size_hint = (0.05, 0.05), pos_hint = {"top":1},source = "BackButton.png")
        b.bind(on_press=lambda instance, button_id="i": gotomainscreen(button_id))
        Box = BoxLayout()
        #self.add_widget(b) 
        self.image = Image()
        #self.add_widget(self.image)
        Box.add_widget(b)
        Box.add_widget(self.image)
        self.add_widget(Box)
        self.event = Clock.schedule_interval(self.update, 1.0 / 30.0) 

        def gotomainscreen(x):
            app = App.get_running_app()
            app.root.current = "Main"

    def update(self, dt):
        ret, frame = self.capture.read()
        if ret:
            buf = cv2.flip(frame, 0).tobytes()  
            texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
            texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
            self.image.texture = texture

    def on_leave(self):
        self.capture.release()
        self.event.cancel()
        cv2.destroyAllWindows()

class ScreenMan(ScreenManager):
    pass

class IPCAMApp(App):
    def build(self):
        sm = ScreenMan()
        sm.add_widget(MainScreen(name="Main"))
        sm.add_widget(StreamView(name="Stream"))
        sm.current = "Main" 
        return sm
    

if __name__ == "__main__":
    IPCAMApp().run()