# Analyzes an object and outputs numeric properties

import cv2
import numpy as np
from . import fatal_error
from . import print_image
from . import plot_image
from . import rgb2gray_hsv
from . import find_objects
from . import binary_threshold
from . import define_roi
from . import roi_objects
from . import object_composition


def report_size_marker_area(img, shape, device, debug, marker='define', x_adj=0, y_adj=0, w_adj=0, h_adj=0,
                            base='white', objcolor='dark', thresh_channel=None, thresh=None, filename=False):
    """Outputs numeric properties for an input object (contour or grouped contours).

    Inputs:
    img             = image object (most likely the original), color(RGB)
    shape           = 'rectangle', 'circle', 'ellipse'
    device          = device number. Used to count steps in the pipeline
    debug           = None, print, or plot. Print = save to file, Plot = print to screen.
    marker          = define or detect, if define it means you set an area, if detect it means you want to
                      detect within an area
    x_adj           = x position of shape, integer
    y_adj           = y position of shape, integer
    w_adj           = width
    h_adj           = height
    base            = background color 'white' is default
    objcolor        = object color is 'dark' or 'light'
    thresh_channel  = 'h', 's','v'
    thresh          = integer value
    filename        = name of file

    Returns:
    device          = device number
    marker_header    = shape data table headers
    marker_data      = shape data table values
    analysis_images = list of output images

    :param img: numpy array
    :param shape: str
    :param device: int
    :param debug: str
    :param marker: str
    :param x_adj:int
    :param y_adj:int
    :param w_adj:int
    :param h_adj:int
    :param h_adj:int
    :param base:str
    :param objcolor: str
    :param thresh_channel:str
    :param thresh:int
    :param filename: str
    :return: device: int
    :return: marker_header: str
    :return: marker_data: int
    :return: analysis_images: list
    """

    device += 1
    ori_img = np.copy(img)
    if len(np.shape(img)) == 3:
        ix, iy, iz = np.shape(img)
    else:
        ix, iy = np.shape(img)

    size = ix, iy
    roi_background = np.zeros(size, dtype=np.uint8)
    roi_size = (ix - 5), (iy - 5)
    roi = np.zeros(roi_size, dtype=np.uint8)
    roi1 = roi + 1
    roi_contour, roi_heirarchy = cv2.findContours(roi1, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(roi_background, roi_contour[0], -1, (255, 0, 0), 5)

    if (x_adj > 0 and w_adj > 0) or (y_adj > 0 and h_adj > 0):
        fatal_error('Adjusted ROI position is out of frame, this will cause problems in detecting objects')