import numpy as np
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Widget_Img(QWidget):
    '''
    Will keep imgRaw, imgVis and imgQmap
    '''
    def __init__(self, img):
        super().__init__()
        '''attr'''
        self.img_raw = img
        self.img_vis = img[:,:,:3].copy()
    def make_rgb_img(self, img):
        h, w = img.shape[0], img.shape[1]
        qImg = QImage(img.astype(np.uint8), w, h, w*3, QImage.Format_RGB888)
        self.qimg = QPixmap(qImg)
    def make_bin_img(self, img):
        h, w = img.shape[0], img.shape[1]
        qImg = QImage(img.astype(np.uint8), w, h, w*1, QImage.Format_Indexed8)
        qImg.setColor(0, qRgb(60, 5, 74))
        qImg.setColor(1, qRgb(241, 225, 29))
        self.qimg = QPixmap(qImg)
    def make_idx8_img(self, img, k):
        colormap = [qRgb(60, 5, 74),
                    qRgb(241, 225, 29),
                    qRgb(52, 177, 106),
                    qRgb(46, 85, 130),
                    qRgb(224, 79, 59),
                    qRgb(247, 248, 177)]
        h, w = img.shape[0], img.shape[1]
        qImg = QImage(img.astype(np.uint8), w, h, w*1, QImage.Format_Indexed8)
        for i in range(k):
            qImg.setColor(i, colormap[i])
        self.qimg = QPixmap(qImg)
    def paintImage(self, painter):
        painter.setRenderHint(QPainter.Antialiasing, True)
        self.size_img = self.qimg.size().scaled(self.rect().size(), Qt.KeepAspectRatio)
        if self.size_img.width()==self.width():
            self.is_fit_width = True
            self.pt_st_img = int((self.height()-self.size_img.height())/2)
            painter.drawPixmap(0, self.pt_st_img, self.size_img.width(), self.size_img.height(), self.qimg)
        elif self.size_img.height()==self.height():
            self.is_fit_width = False
            self.pt_st_img = int((self.width()-self.size_img.width())/2)
            painter.drawPixmap(self.pt_st_img, 0, self.size_img.width(), self.size_img.height(), self.qimg)

def save_img(qimg, path):
    w, h = qimg.width(), qimg.height()
    file = QFile(path+".jpg");
    file.open(QIODevice.WriteOnly);
    qimg.save(file, "JPG");

def magnifying_glass(widget, pos, area=400, zoom=2, isCursor=True):
    size = int(area/zoom)
    pixmap = widget.grab(QRect(pos.x()-int(size/2), pos.y()-int(size/2), size, size))
    pixmap = pixmap.scaled(area, area)
    painter = QPainter(pixmap)
    pen = QPen()
    pen.setWidth(2)
    pen.setColor(Qt.black)
    painter.setPen(pen)
    painter.drawRect(0, 0, int(area), int(area))
    if isCursor:
        pen.setWidth(3)
        pen.setColor(Qt.red)
        painter.setPen(pen)
        size_m = 10
        pt_paint = int(area/2)
        painter.drawLine(pt_paint-size_m, pt_paint, pt_paint+size_m, pt_paint)
        painter.drawLine(pt_paint, pt_paint-size_m, pt_paint, pt_paint+size_m)
    painter.end()
    cursor = QCursor(pixmap)
    widget.setCursor(cursor)

def draw_cross(x, y, painter, size_mark=3):
    l1_st_x, l1_st_y = x-size_mark, y-size_mark
    l1_ed_x, l1_ed_y = x+size_mark, y+size_mark
    l2_st_x, l2_st_y = x-size_mark, y+size_mark
    l2_ed_x, l2_ed_y = x+size_mark, y-size_mark
    painter.drawLine(l1_st_x, l1_st_y, l1_ed_x, l1_ed_y)
    painter.drawLine(l2_st_x, l2_st_y, l2_ed_x, l2_ed_y)

def draw_triangle(x, y, dir, painter, range=7, peak=30):
    path = QPainterPath()
    path.moveTo(x, y)
    if dir=='North':
        path.lineTo(x-range, y+peak)
        path.lineTo(x+range, y+peak)
    elif dir=='South':
        path.lineTo(x-range, y-peak)
        path.lineTo(x+range, y-peak)
    elif dir=='West':
        path.lineTo(x+peak, y-range)
        path.lineTo(x+peak, y+range)
    elif dir=='East':
        path.lineTo(x-peak, y-range)
        path.lineTo(x-peak, y+range)
    path.lineTo(x, y)
    painter.drawPath(path)
