import sys
import kivy
kivy.require('1.9.1')
import os
import time
import requests
import webbrowser
import plyer
import bs4
from kivy.app import App
from kivy.config import Config  
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from plyer import call

class ImportFixer(object):
    def __init__(self, mname):
        self.mname = mname

    def find_module(self, name, path=None):
        if name == self.mname:
            return self
        return None

    def load_module(self, name):
        import _htmlparser as module
        module.__name__ = name
        return module

sys.meta_path = [ImportFixer('bs4.builder._htmlparser')]
from bs4 import BeautifulSoup

Config.set('graphics', 'resizable', False)  

Builder.load_string("""
<MyLabel1>
    text_size: self.size
    shorten: False
    markup: True
    background_color: 0.7, 0.7, 0.7, .2
    canvas.before:
        Color:
            rgba: self.background_color
        Rectangle:
            pos: self.pos
            size: self.width, 242
<MyLabel2>
    text_size: self.size
    shorten: False
    markup: True
    background_color: 0.9, 0.9, 0.9, .3
    canvas.before:
        Color:
            rgba: self.background_color
        Rectangle:
            pos: self.pos
            size: self.width, 242
<MyTitle>
    markup: True
""")

class MyLabel1(Label):
    """
    A Label class with dark grey background, markups and wrapping
    """
    pass

class MyLabel2(Label):
    """
    A Label class with light grey background, markups and wrapping
    """
    pass

class MyTitle(Label):
    """
    A Label class with markups for the main title
    """
    pass

class ConceptruelleApp(App):

    def build(self):
        """
        Scrap ConcepTruelle's website agenda and put it on screen
        4 buttons give access to website, social medias and phone number
        """
        def phonecall(self, *args):
            """
            Make a Phone Call on Android with Plyer
            """
            phone_number = 9999999999
            call.makecall(tel=phone_number)
         
        def website(self):
            """
            Open ConcepTruelle's website
            """
            webbrowser.open("http://conceptruelle.fr/")

        def facebook(self):
            """
            Open ConcepTruelle's Facebook page
            """
            webbrowser.open("https://www.facebook.com/ConcepTruelle")

        def instagram(self):
            """
            Open ConcepTruelle's Instagram page
            """
            webbrowser.open("https://www.instagram.com/conceptruelle")
        
        # Main application : definition of title, labels and buttons put on a Float Layout
        fl = FloatLayout()
        lbl0 = MyTitle(text = "  [size=90][color=000000]L\'[/color][color=dd9514][i]Agenda[/i][/color] [color=000000][b]ConcepTruelle[/b][/size][/color]", markup=True, size_hint =(1, .1), pos_hint ={'center_x':.5, 'center_y':.97})
        lbl1 = MyLabel1(text = event_title_1 + "\n" + agenda1, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':1.04})
        lbl2 = MyLabel2(text = event_title_2 + "\n" + agenda2, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':.87})
        lbl3 = MyLabel1(text = event_title_3 + "\n" + agenda3, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':.70})
        lbl4 = MyLabel2(text = event_title_4 + "\n" + agenda4, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':.53})
        lbl5 = MyLabel1(text = event_title_5 + "\n" + agenda5, size_hint =(1, .5), pos_hint ={'center_x':.5, 'center_y':.36})
        fl.add_widget(lbl0)
        fl.add_widget(lbl1)
        fl.add_widget(lbl2)
        fl.add_widget(lbl3)
        fl.add_widget(lbl4)
        fl.add_widget(lbl5)
        btn1 = Button(text = "Appel", size_hint =(.25, .1), pos_hint ={'center_x':.125, 'center_y':.05})
        btn1.bind(on_press=phonecall)
        btn2 = Button(text = "Site Web", size_hint =(.25, .1), pos_hint ={'center_x':.375, 'center_y':.05})
        btn2.bind(on_press=website)
        btn3 = Button(text = "Facebook", size_hint =(.25, .1), pos_hint ={'center_x':.625, 'center_y':.05})
        btn3.bind(on_press=facebook)
        btn4 = Button(text = "Instagram", size_hint =(.25, .1), pos_hint ={'center_x':.875, 'center_y':.05})
        btn4.bind(on_press=instagram)
        fl.add_widget(btn1)
        fl.add_widget(btn2)
        fl.add_widget(btn3)
        fl.add_widget(btn4)
        return fl

if __name__ == '__main__':

    agenda_list = list()
    urlpage = "http://conceptruelle.fr/index.php/notre-agenda"
    page = requests.get(urlpage)
    soup = BeautifulSoup(page.content, 'html.parser')
    # titles soup
    for i in range(len(soup.find_all(class_="eventtitle ic-event-title ic-clearfix"))):
        agenda_list.append(soup.find_all(class_="eventtitle ic-event-title ic-clearfix")[i].get_text())
    # description soup
    for i in range(len(soup.find_all(class_="descshort ic-descshort"))):
        agenda_list.append(soup.find_all(class_="descshort ic-descshort")[i].get_text())
    # day soup
    for i in range(len(soup.find_all(class_="nextdate ic-next-date ic-clearfix"))):
        agenda_list.append(soup.find_all(class_="nextdate ic-next-date ic-clearfix")[i].get_text())
    # location soup
    for i in range(len(soup.find_all(class_="place ic-place"))):
        agenda_list.append(soup.find_all(class_="place ic-place")[i].get_text())

    # Modifying the list to fit requirements
    for i in range(len(agenda_list)):
        agenda_list[i] = agenda_list[i].replace('\t', '')
    for i in range(len(agenda_list)):
        agenda_list[i] = agenda_list[i].replace('\n', '')
    for i in range(len(agenda_list)):
        agenda_list[i] = agenda_list[i].replace('Atelier', ' [color=8113bd]- Atelier[/color] ')
    for i in range(len(agenda_list)):
        agenda_list[i] = agenda_list[i].replace('Stage', ' [color=0aa7f5]- Stage[/color] ')
    for i in range(len(agenda_list)):
        agenda_list[i] = agenda_list[i].replace('Collab', ' [color=26df00]- Collab\'[/color] ')
    
    print(agenda_list)
    
    # creating titles and labels with the list
    event_title_1 = str("[size=50][color=dd9514] [b]" + agenda_list[0] + "[/b][/color][/size]")
    event_title_2 = str("[size=50][color=dd9514] [b]" + agenda_list[1] + "[/b][/color][/size]")
    event_title_3 = str("[size=50][color=dd9514] [b]" + agenda_list[2] + "[/b][/color][/size]")
    event_title_4 = str("[size=50][color=dd9514] [b]" + agenda_list[3] + "[/b][/color][/size]")
    event_title_5 = str("[size=50][color=dd9514] [b]" + agenda_list[4] + "[/b][/color][/size]")
    agenda1 = str("[color=000000][i] " + agenda_list[5] + "[/i]\n " + agenda_list[10] + "\n Lieu : " + agenda_list[15] + "[/color]")
    agenda2 = str("[color=000000][i] " + agenda_list[6] + "[/i]\n " + agenda_list[11] + "\n Lieu : " + agenda_list[16] + "[/color]")
    agenda3 = str("[color=000000][i] " + agenda_list[7] + "[/i]\n " + agenda_list[12] + "\n Lieu : " + agenda_list[17] + "[/color]")
    agenda4 = str("[color=000000][i] " + agenda_list[8] + "[/i]\n " + agenda_list[13] + "\n Lieu : " + agenda_list[18] + "[/color]")
    agenda5 = str("[color=000000][i] " + agenda_list[9] + "[/i]\n " + agenda_list[14] + "\n Lieu : " + agenda_list[19] + "[/color]")
    
    
    # Define main window's background color
    Window.clearcolor = (1, 1, 1, 1)
    
    # Call the main application
    ConceptruelleApp().run()
