import logging
import os
import sys
from inspect import currentframe, getframeinfo
from tkinter.filedialog import askopenfilenames
sys.path.append('../..')

import cv2
from src.image.imnp import ImageNP
from src.support.tkconvert import TkConverter
from src.view.removal_app import RemovalViewer

__FILE__ = os.path.abspath(getframeinfo(currentframe()).filename)
LOGGER = logging.getLogger(__name__)

class RemovalAction(RemovalViewer):
    def __init__(self):
        super().__init__()
        self._current_image_info = {}
        self._image_queue = []

    @property
    def current_image(self):
        if self._current_image_info and 'path' in self._current_image_info:
            return self._current_image_info['path']
        else:
            return False

    def _check_and_update_photo(self, target_widget, img):
        try:
            self._tmp_photo = TkConverter.cv2_to_photo(img)
            target_widget.config(image=self._tmp_photo)
        except Exception as e:
            self._default_photo = ImageNP.generate_checkboard((self._im_h, self._im_w), 10)
            self._default_photo = TkConverter.ndarray_to_photo(self._default_photo)
            target_widget.config(image=self._tmp_photo)

    # update current image
    def _update_current_image(self, index):
        if self._image_queue is None or not self._image_queue:
            LOGGER.warning('No images in the queue')
        elif index < 0 or index >= len(self._image_queue):
            LOGGER.error('Image queue out of index')
        else:
            self._current_image_info = {
                'index': index,
                'path': self._image_queue[index],
                'image': cv2.imread(self._image_queue[index])
            }
            LOGGER.info('Read image - {}'.format(self._current_image_info['path']))
            self._current_image_info['size'] = self._current_image_info['image'].shape[:2]
            self._current_image_info['image'] = self.auto_resize(self._current_image_info['image'])
            self._current_image_info['resize'] = self._current_image_info['image'].shape[:2]
            self.label_resize.config(text=u'原有尺寸 {}X{} -> 顯示尺寸 {}X{}'.format(
                *self._current_image_info['size'][::-1], *self._current_image_info['resize'][::-1]
            ))

    # open filedialog to get input image paths
    def input_images(self):
        initdir = os.path.abspath(os.path.join(__FILE__, '../../../'))
        paths = askopenfilenames(
            title=u'請選擇要去被的圖片',
            filetypes=[('JPG file (*.jpg)', '*jpg'),
                       ('JPEG file (*.jpeg)', '*.jpeg'),
                       ('PNG file (*.png)', '*.png')],
            initialdir=initdir,
            parent=self.root
        )

        if paths:
            self._image_queue = list(paths)

            # update first image to input panel
            self._update_current_image(index=0)
            self._check_and_update_photo(self.label_panel_image, self._current_image_info['image'])

    # auto fit the image hight from original image to resize image
    def auto_resize(self, image, ratio=0.5):
        screen_h = self.root.winfo_screenheight()
        screen_w = self.root.winfo_screenwidth()
        image_h, image_w, image_channel = image.shape
        resize_h = screen_h * ratio
        resize_w = (resize_h / image_h) * image_w
        resize_h, resize_w = int(resize_h), int(resize_w)
        image = cv2.resize(image, (resize_w, resize_h),interpolation=cv2.INTER_AREA)
        LOGGER.info('resize image from {}x{} to {}x{}'.format(
            image_w, image_h, int(resize_w), int(resize_h)
        ))
        return image


if __name__ == '__main__':
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s %(filename)12s:L%(lineno)3s [%(levelname)8s] %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S',
        stream=sys.stdout
    )

    removal_action = RemovalAction()
    removal_action.input_images()
    removal_action.mainloop()

@@ -392,16 +392,35 @@
 _name__)
+%0ASTATE = %5B'browse'%5D
 %0A%0Aclass 
@@ -500,16 +500,47 @@
 nit__()%0A
+        self._image_queue = %5B%5D%0A
         
@@ -583,32 +583,36 @@
   self._
-image_queue = %5B%5D
+current_state = None
 %0A%0A    @p
@@ -816,16 +816,61 @@
  False%0A%0A
+    # check and update image to given widget%0A
     def 
@@ -1323,72 +1323,624 @@
   # 
-update current image%0A    def _update_current_image(self, index):
+switch to state and update related action%0A    def _switch_state(self, state):%0A        if state is None or not state or state not in STATE:%0A            LOGGER.error('%7B%7D not in standard state'.fornat(state))%0A        elif state == 'browse':%0A            self.label_state.config(text=u'%E7%8F%BE%E5%9C%A8%E6%A8%A1%E5%BC%8F: %E7%80%8F%E8%A6%BD')%0A%0A    # update current image%0A    def _update_current_image(self, index):%0A        '''%0A        - record current image index, path%0A        - read image%0A        - resize image%0A        - record current image resize info%0A        - update size message%0A        - reset algorithm parameters%0A        - reset state to browse%0A        '''
 %0A   
@@ -2936,16 +2936,242 @@
       ))
+%0A            self._reset_parameter()%0A            self._switch_state(state='browse')%0A%0A    # reset algorithm parameter%0A    def _reset_parameter(self):%0A        self.val_scale_threshold.set(0.85)%0A        self.val_scale_iter.set(5)
 %0A%0A    # 

reset paras and swich to browse state