# -*- coding: utf-8 -*-
# ------------------------------------------------------------
# TheGroove360 / XBMC Plugin
# Canale 
# ------------------------------------------------------------
from platformcode import logger
from core import scrapertools
from core import servertools
from core.item import Item


def mainlist(item):
    logger.info()

    itemlist = []
    itemlist.append( Item(channel=item.channel, action="search", title="Inserire l'URL [Link a server / download]"))
    itemlist.append( Item(channel=item.channel, action="search", title="Inserire l'URL [Link diretto al video]"))
    itemlist.append( Item(channel=item.channel, action="search", title="Inserire l'URL [Ricerca link in un URL]"))

    return itemlist

# Al llamarse "search" la función, el launcher pide un texto a buscar y lo añade como parámetro
def search(item,texto):
    logger.info("texto="+texto)

    if not texto.startswith("http://"):
        texto = "http://"+texto

    itemlist = []

    if "servidor" in item.title:
        itemlist = servertools.find_video_items(data=texto)
        for item in itemlist:
            item.channel="tengourl"
            item.action="play"
    elif "directo" in item.title:
        itemlist.append( Item(channel=item.channel, action="play", url=texto, server="directo", title="Ver enlace directo"))
    else:
        data = scrapertools.downloadpage(texto)
        itemlist = servertools.find_video_items(data=data)
        for item in itemlist:
            item.channel="tengourl"
            item.action="play"

    if len(itemlist)==0:
        itemlist.append( Item(channel=item.channel, action="search", title="Non c'è uno stream compatibile per questa URL"))
    
    return itemlist
