from gamera.core import *
from gamera.core import RGBPixel,FloatPoint

class ImageView():
    '''Graficky zaznam segmentu
    '''
    def __init__(self):
        pass


    def drawothers(self, seg, img):
        '''Zakresleni jakychkoli sementu
        '''
        imgrgb = img.to_rgb()
        for c in seg:
            imgrgb.draw_hollow_rect(FloatPoint(c.ul_x, c.ul_y), FloatPoint(c.lr_x, c.lr_y),RGBPixel(255, 0, 0))
        return imgrgb

    def drawlines(self, seg, img):
        '''Zakresleni linek
        '''
        imgrgb = img.to_rgb()
        for c in seg:
            imgrgb.draw_filled_rect(FloatPoint(c.ul_x, c.ul_y), FloatPoint(c.lr_x, c.lr_y),RGBPixel(0, 255, 0))
        return imgrgb


    def drawheads(self, heads, img):
        '''Zakresleni not - hlavicek, linek, tecek a praporku
        '''
        imgrgb = img.to_rgb()
        if heads or heads != None:
            for head in heads:
                imgrgb.draw_hollow_rect(FloatPoint(head.seghead.ul_x, head.seghead.ul_y),
                                             FloatPoint(head.seghead.lr_x, head.seghead.lr_y),
                                             RGBPixel(255, 0, 0), 1)

                imgrgb.draw_filled_rect(FloatPoint(head.segline.ul_x, head.segline.ul_y),
                                        FloatPoint(head.segline.lr_x, head.segline.lr_y),
                                        RGBPixel(0, 255, 0))

                if (head.dir == 1):
                    imgrgb.draw_line(FloatPoint(head.segline.center_x, head.segline.lr_y),
                                          FloatPoint(head.seghead.center_x, head.seghead.center_y),
                                          RGBPixel(0, 0, 255), 1)
                    imgrgb.draw_text(FloatPoint(head.segline.center_x, head.segline.lr_y), str(head.dur),
                                     RGBPixel(255, 0, 0), 6, 2, False, True, 0)

                elif (head.dir == 2):
                    imgrgb.draw_line(FloatPoint(head.segline.center_x, head.segline.ul_y),
                                          FloatPoint(head.seghead.center_x, head.seghead.center_y),
                                          RGBPixel(0, 0, 255), 1)
                    imgrgb.draw_text(FloatPoint(head.segline.center_x, head.segline.ul_y), str(head.dur),
                                     RGBPixel(255, 0, 0), 6, 2, False, True, 0)

                if(head.dot):
                    imgrgb.draw_hollow_rect(FloatPoint(head.dot.ul_x, head.dot.ul_y),
                                            FloatPoint(head.dot.lr_x, head.dot.lr_y),
                                            RGBPixel(0, 0, 255), 1)

                if (head.flag is not None):
                    imgrgb.draw_hollow_rect(FloatPoint(head.flag.ul_x, head.flag.ul_y),
                                            FloatPoint(head.flag.lr_x, head.flag.lr_y),
                                            RGBPixel(255, 0, 255), 1)


            #rgbimg.draw_hollow_rect(FloatPoint(ccc[head[1]].ul_x + tempoffset, imgrscc[snote[0]].ul_y),
            #                        FloatPoint(ccc[head[1]].ul_x + tempoffset,
            #                                   ccc[head[1]].lr_y - (ccc[head[1]].height / 2)),
            #                        RGBPixel(0, 0, 255), 1)

            #rgbimg.draw_hollow_rect(
            #    FloatPoint(ccc[head[1]].ul_x + tempoffset, ccc[head[1]].lr_y - (ccc[head[1]].height / 2)),
            #    FloatPoint(ccc[head[1]].ul_x + tempoffset, imgrscc[snote[0]].lr_y),
            #    RGBPixel(0, 0, 255), 1)

        return imgrgb


    def drawhalfheads(self, heads, img):
        '''Zakresleni pulovych not - hlavicek, linek, tecek
        '''
        imgrgb = img.to_rgb()
        for head in heads:
            imgrgb.draw_hollow_rect(FloatPoint(head.seghead.ul_x, head.seghead.ul_y),
                                         FloatPoint(head.seghead.lr_x, head.seghead.lr_y),
                                         RGBPixel(255, 0, 0), 1)
            if (head.dir == 1):
                imgrgb.draw_line(FloatPoint(head.segline.center_x, head.segline.lr_y),
                                      FloatPoint(head.seghead.center_x, head.seghead.center_y),
                                      RGBPixel(0, 0, 255), 1)

            elif (head.dir == 2):
                imgrgb.draw_line(FloatPoint(head.segline.center_x, head.segline.ul_y),
                                      FloatPoint(head.seghead.center_x, head.seghead.center_y),
                                      RGBPixel(0, 0, 255), 1)

        return imgrgb


    def drawBeams(self, beams, img):
        '''Zakresleni tramcu
        '''
        imgrgb = img.to_rgb()
        for beam in beams:
            for part in beam.beam:
                imgrgb.draw_hollow_rect(FloatPoint(part.ul_x, part.ul_y),
                                      FloatPoint(part.lr_x, part.lr_y),
                                       RGBPixel(0, 255, 255), 1)
        return imgrgb


    def save(self):
        '''Ulozeni obrazku
        '''
        self.imgrgb.save_PNG('../../../tests/img/rgbnew.png')