import os
import sys

import time
import threading

from termcolor import colored, cprint
from common import pussycat_filter_file


class Receiver:
    data = None
    refreshing = None
    current_filter = None

    def __init__(self):
        self.data = list()
        self.refreshing = False
        self.current_filter = ''

    def print_line(self, line):
        out = None
        if line.__contains__(" D/") or line.__contains__(" D "):
            out = colored(line, 'grey')
        if line.__contains__(" D/") or line.__contains__(" D "):
            out = colored(line, 'white')
        if line.__contains__(" I/") or line.__contains__(" I "):
            out = colored(line, 'cyan')
        if line.__contains__(" W/") or line.__contains__(" W "):
            out = colored(line, 'yellow')
        if line.__contains__(" E/") or line.__contains__(" E "):
            out = colored(line, 'red')
        cprint(out)

    def receiver(self):
        while True:
            received = sys.stdin.readline().rstrip('\n')
            if received:
                self.data.append(received)
                if not self.refreshing and self.filter_ok(received):
                    self.print_line(received)
            else:
                time.sleep(1)
        return

    def logcat_filter(self):
        while True:
            if not os.path.isfile(pussycat_filter_file):
                if self.current_filter != '':
                    self.current_filter = ''
                    self.apply_filter()
            else:
                with open(pussycat_filter_file) as filter_file:
                    filter_to_apply = filter_file.readlines()
                    filter_file.close()
                    if filter_to_apply == self.current_filter:
                        pass
                    self.current_filter = filter_to_apply
                    self.apply_filter()
        time.sleep(1)

    def apply_filter(self):
        self.refreshing = True
        print(chr(27) + "[2J")
        for item in enumerate(self.data):
            if self.filter_ok(item):
                self.print_line(item)
        self.refreshing = False

    def filter_ok(self, received):
        if self.current_filter != '':
            if self.current_filter.__contains__(","):
                # TODO: TBD.
                pass
            else:
                return received.__contains__(self.current_filter)
        return True

    def start(self):
        receiver_thread = threading.Thread(target=self.receiver())
        receiver_thread.start()

        data_filter_thread = threading.Thread(target=self.logcat_filter())
        data_filter_thread.start()


receiver = Receiver()
receiver.start()

@@ -359,27 +359,8 @@
 e):%0A
-        out = None%0A
     
@@ -424,38 +424,39 @@
 %22):%0A            
-out = 
+cprint(
 colored(line, 'g
@@ -460,16 +460,36 @@
  'grey')
+)%0A            return
 %0A       
@@ -550,38 +550,39 @@
 %22):%0A            
-out = 
+cprint(
 colored(line, 'w
@@ -587,16 +587,36 @@
 'white')
+)%0A            return
 %0A       
@@ -677,38 +677,39 @@
 %22):%0A            
-out = 
+cprint(
 colored(line, 'c
@@ -713,16 +713,36 @@
  'cyan')
+)%0A            return
 %0A       
@@ -803,38 +803,39 @@
 %22):%0A            
-out = 
+cprint(
 colored(line, 'y
@@ -841,16 +841,36 @@
 yellow')
+)%0A            return
 %0A       
@@ -943,14 +943,15 @@
     
-out = 
+cprint(
 colo
@@ -966,25 +966,28 @@
 , 'red')
-%0A
+)%0A  
         
 cprint(o
@@ -982,19 +982,16 @@
     
-cprint(out)
+  return
 %0A%0A  
@@ -1079,20 +1079,47 @@
 ne()
+%0A            received = received
 .
-r
 strip(
-'%5Cn'
 )%0A  

Work in progress.