"""Small vector icons drawn in the active Qt palette."""
import math

from PySide6.QtCore import QPointF, QRectF, QSize, Qt
from PySide6.QtGui import QColor, QIcon, QPainter, QPainterPath, QPen, QPixmap


def line_icon(name, color):
    pixmap = QPixmap(QSize(48, 48))
    pixmap.setDevicePixelRatio(2)
    pixmap.fill(Qt.GlobalColor.transparent)
    p = QPainter(pixmap)
    p.setRenderHint(QPainter.RenderHint.Antialiasing)
    p.setPen(QPen(QColor(color), 1.6, Qt.PenStyle.SolidLine,
                  Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin))
    if name == 'sun':
        p.drawEllipse(QPointF(12, 12), 4, 4)
        for i in range(8):
            a = i*math.pi/4
            v = QPointF(math.cos(a), math.sin(a))
            p.drawLine(QPointF(12, 12)+v*7, QPointF(12, 12)+v*9)
    elif name == 'moon':
        path = QPainterPath(QPointF(12.9, 3.5))
        path.cubicTo(3, 2.5, 1.1, 17.1, 10.1, 20)
        path.cubicTo(14.8, 21.6, 18.8, 18.8, 20.2, 14.9)
        path.cubicTo(13.7, 16.2, 9.4, 9.8, 12.9, 3.5)
        p.drawPath(path)
    elif name == 'reset':
        p.drawArc(QRectF(4,4,16,16),35*16,-285*16)
        p.drawLine(QPointF(3,5),QPointF(3,11))
        p.drawLine(QPointF(3,11),QPointF(9,11))
    elif name == 'log':
        p.drawRoundedRect(QRectF(5,3,14,18),2,2)
        for y in (8,12,16):
            p.drawLine(QPointF(8,y),QPointF(16,y))
    p.end()
    return QIcon(pixmap)


def set_icon(button, name, label, color):
    button.setProperty('iconOnly',True)
    button.setIcon(line_icon(name,color))
    button.setIconSize(QSize(20,20))
    button.setToolTip(label)
    button.setAccessibleName(label)
