# -*- coding: utf-8 -*-
from logging import getLogger
from enum import Enum, unique
from pathlib import Path
from io import BytesIO
import os
#
import cv2
import numpy as np
#
from serializer import Serializer
from hsvcolor import HSVcolor
from events import Filters
# pylint: disable=C0103
logger = getLogger('myapp.tweetbot')

@unique
class ImageType(Enum):
    RAW = 1
    PLAN = 8
class DataProcessor(object):
    """
        DataProcessor is image convert
    """
    def __init__(self, media, image_type, save_image=False):
        self.__media = media
        self.image_type = image_type
        self.color = None
        self.__hsv = None
        self.save_image = save_image
        # image => ocr image.
        self.filter = Filters()
        self.filter += self.base_filtered
        if image_type == ImageType.RAW:
            self.filter += self.filtered
        else:
            self.filter += self.clipping_filtered
    @property
    def media(self):
        return self.__media
    @property
    def hsv(self):
        if self.__hsv is None:
            self.__hsv = cv2.cvtColor(self.color, cv2.COLOR_BGR2HSV)
        return self.__hsv
    def prepare(self):
        """
            @return {color} image
            @exception FileNotFoundError
        """
        filename = self.media
        zoom = 2
        c = cv2.imread(filename)
        if c is None:
            raise FileNotFoundError(self.media)
        self.color = cv2.resize(c, (c.shape[1]*zoom, c.shape[0]*zoom), interpolation=cv2.INTER_CUBIC)
        return self.color
    def batch(self):
        """
            @return {binary} image
        """
        result = self.filter(self.color)
        if self.save_image:
            cv2.imwrite('../temp/ocr_{0}'.format(os.path.basename(self.media)), result)
        return result
    def base_filtered(self, sender, ev):
        result = cv2.cvtColor(sender, cv2.COLOR_BGR2GRAY)
        result = cv2.adaptiveThreshold(result, 255,
                                       cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 3)
        if self.save_image:
            cv2.imwrite('../temp/color{0}'.format(os.path.basename(self.media)), sender)
        return result
    def clipping_filtered(self, sender, ev):
        stream = sender
        binary = stream
        # white color
        white = self.__getWhiteMasking(self.hsv)
        binary = cv2.bitwise_and(binary, binary, mask=white)
        height, width = stream.shape[:2]
        cv2.rectangle(binary, (0, 0), (min(1000, width), height), (0, 0, 0), -1)
        return binary
    def filtered(self, sender, ev):
        stream = sender
        binary = stream

        # white color
        white = self.__getWhiteMasking(self.hsv)
        binary = cv2.bitwise_and(binary, binary, mask=white)
        lower, upper = HSVcolor(0, 0, 0), HSVcolor(30, 66, 255)
        binary = cv2.bitwise_and(binary, self.__inRange(self.hsv, lower, upper))
        # image out range fill
        height, width = stream.shape[:2]
        clip_rect = Serializer.load_json('../resource/ocr.json')['clipping']
        start_x, start_y = clip_rect['start']
        end_x, end_y = clip_rect['end']
        binary = binary[start_y:end_x, start_x:end_y]
        return binary
    def bitwise_not(self, binary, mask_range):
        lower, upper = mask_range
        return cv2.bitwise_not(binary, binary, mask=self.__inRange(self.hsv, lower, upper))
    def __getWhiteMasking(self, hsv):
        sensitivity = 15
        lower = HSVcolor(0, 0, 255 - sensitivity)
        upper = HSVcolor(255, sensitivity, 255)
        return self.__inRange(hsv, lower, upper)
    def __inRange(self, hsv, lower, upper):
        return cv2.inRange(hsv, lower.to_np(), upper.to_np())
    def tobinary(self, binary):
        """
            convert cv2.binary => Memory
            @param {cv2.binary} image
            @return {io.BytesIO} Memory
        """
        baseName = Path(self.media)
        ret, buf = cv2.imencode(baseName.suffix, binary)
        return BytesIO(np.array(buf).tostring())

@@ -774,32 +774,76 @@
 f.base_filtered%0A
+        self.filter += self.addWhiteMasking%0A
         if image
@@ -2212,24 +2212,121 @@
 )), sender)%0A
+            cv2.imwrite('../temp/base_filtered%7B0%7D'.format(os.path.basename(self.media)), result)%0A
         retu
@@ -2343,33 +2343,31 @@
     def 
-clipping_filtered
+addWhiteMasking
 (self, s
@@ -2550,32 +2550,431 @@
 ry, mask=white)%0A
+        cv2.imwrite('../temp/addWhiteMasking%7B0%7D'.format(os.path.basename(self.media)), binary)%0A        return binary%0A    def image_clipping(self, sender, ev):%0A        pass%0A    def clipping_filtered(self, sender, ev):%0A        stream = sender%0A        binary = stream%0A        # white color%0A        #white = self.__getWhiteMasking(self.hsv)%0A        #binary = cv2.bitwise_and(binary, binary, mask=white)%0A
         height, 
@@ -3208,32 +3208,33 @@
 e color%0A        
+#
 white = self.__g
@@ -3258,32 +3258,33 @@
 lf.hsv)%0A        
+#
 binary = cv2.bit

add self.addWhiteMasking