#!/usr/bin/env python
# coding=utf8
#
# This file is part of PlayStream - enigma2 plugin to play video streams from various sources
# Copyright (c) 2016 ivars777 (ivars777@gmail.com)
# Distributed under the GNU GPL v3. For full terms see http://www.gnu.org/licenses/gpl-3.0.en.html
#
import os.path,re
import collections
from SourceBase import SourceBase

os.path.dirname(os.path.abspath(__file__))
class Source(SourceBase):
    
    def __init__(self,country="lv"):
        self.name = "config"
        self.country=country
        cur_directory = os.path.dirname(os.path.abspath(__file__))
        self.streams_file = os.path.join(cur_directory,"streams.cfg")
        self.lists = collections.OrderedDict()
        self.read_streams()
         
    def get_content(self, data):
        self.read_streams()
        if "::" in data:
            data = data.split("::")[1]
        if not data in self.lists:
            return []
        return self.lists[data]

    def is_video(self,data):
        return False
    
    def read_streams(self):
        for line in open(self.streams_file,"r"):
            r = re.search("^\[(\w+)\]", line)
            if r:
                name = r.group(1)
                self.lists[name] = []
            else:
                if len(line)<10 or line[0] in ("#"): continue
                items = tuple(line.strip().split("|"))
                if len(items)<2:
                    continue
                self.lists[name].append(items)
                
    def write_streams(self):
        f = open(self.streams_file,"w")
        for l in self.lists.keys():
            f.write("[%s]\n"%l)
            for item in self.lists[l]:
                f.write("%s|%s|%s|%s\n"%(item[0],item[1],item[2],item[3]))
            f.write("\n")
        f.close()
    
    def get_lists(self):
        return self.lists.keys()
    
    def get_list_items(self,name):
        return self.lists[name]
    
    def add_list(self,name):
        if not name in self.lists.keys():
            self.lists[name] = []
            
    def del_list(self,name):
        if name in self.lists.keys():
            del self.lists[name]
            
    def add_item(self,name,item,pos=None):
        if name in self.lists.keys():
            if pos==None:
                self.lists[name].append(item)
            else:
                self.lists[name].insert(pos,item)
                
    def del_item(self,name,pos):
        self.lists[name].pop(pos)
    
    def replace_item(self,name,item,pos):
        self.lists[name][pos]=item
            
                           
if __name__ == "__main__":
    c = Source()
    content = c.get_content("home")
    for item in content: print item
    #c.del_item("home",0)
    #c.del_list("favorites")
    
    #c.add_item("home",("My Streams","config::favorites","","Mani saglabātie TV kanāli un video"),0)
    c.replace_item("home",("My Streams","config::my_streams","default","Mani saglabātie TV kanāli un video"),0)
    #c.add_list("favorites")
    #c.add_item("favorites",("..return","back","","Atgriezties atpakaļ"))    
    #c.add_item("favorites",("LTV1","http://streamltv.cloudy.services/ltv/LTV02.smil/playlist.m3u8","picons/latvia1.png", "Latvijas televīzijas 1.kanāls"))
    
    c.write_streams()
    for item in content: print item
    
