import os
import sys

import pytest

from msl.qt import convert, Button, QtWidgets, QtCore, Qt


def test_text():
    b = Button(text='hello')
    assert b.text() == 'hello'
    assert b.icon().isNull()
    assert b.toolButtonStyle() == Qt.ToolButtonTextOnly


def test_icon():
    path = os.path.dirname(__file__) + '/gamma.png'
    gamma_size = QtCore.QSize(191, 291)

    int_val = QtWidgets.QStyle.SP_DriveNetIcon
    icon = convert.to_qicon(int_val)
    sizes = icon.availableSizes()
    if sys.platform == 'win32':
        assert len(sizes) > 1

    b = Button(icon=int_val)
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == sizes[0]
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly

    b = Button(icon=path)
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == gamma_size
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly

    b = Button(icon=convert.icon_to_base64(convert.to_qicon(path)))
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == gamma_size
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly


def test_icon_size():
    int_val = QtWidgets.QStyle.SP_DriveNetIcon
    icon = convert.to_qicon(int_val)
    sizes = icon.availableSizes()
    if sys.platform == 'win32':
        assert len(sizes) > 1

    #
    # specify the size to the get_icon function
    #

    b = Button(icon=convert.to_qicon(int_val))
    assert b.text() == ''
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly
    assert b.iconSize() == sizes[0]

    b = Button(icon=convert.to_qicon(int_val, size=789))
    assert b.iconSize() == QtCore.QSize(789, 789)

    b = Button(icon=convert.to_qicon(int_val, size=3.0))
    # specifying a scale factor will use the largest available size
    assert b.iconSize() == QtCore.QSize(3*sizes[-1].width(), 3*sizes[-1].height())

    b = Button(icon=convert.to_qicon(int_val, size=QtCore.QSize(50, 50)))
    assert b.iconSize() == QtCore.QSize(50, 50)

    for size in [(256,), (256, 256, 256)]:
        with pytest.raises(ValueError, match='(width, height)'):
            Button(icon=convert.to_qicon(int_val, size=size))

    #
    # use the icon_size kwarg
    #

    b = Button(icon=convert.to_qicon(int_val), icon_size=1234)
    assert b.iconSize() == QtCore.QSize(1234, 1234)

    b = Button(icon=convert.to_qicon(int_val), icon_size=3.0)
    # specifying a scale factor will use the largest available size
    assert b.iconSize() == QtCore.QSize(3*sizes[-1].width(), 3*sizes[-1].height())

    b = Button(icon=convert.to_qicon(int_val), icon_size=(312, 312))
    assert b.iconSize() == QtCore.QSize(312, 312)

    b = Button(icon=convert.to_qicon(int_val), icon_size=QtCore.QSize(500, 500))
    assert b.iconSize() == QtCore.QSize(500, 500)

    for size in [(256,), (256, 256, 256)]:
        with pytest.raises(ValueError, match='(width, height)'):
            Button(icon=convert.to_qicon(int_val), icon_size=size)


def test_text_and_icon():
    b = Button(text='hello', icon=QtWidgets.QStyle.SP_DriveNetIcon)
    assert b.text() == 'hello'
    assert not b.icon().isNull()
    assert b.toolButtonStyle() == Qt.ToolButtonTextUnderIcon

    b = Button(text='world', icon=QtWidgets.QStyle.SP_DriveNetIcon, is_text_under_icon=False)
    assert b.text() == 'world'
    assert not b.icon().isNull()
    assert b.toolButtonStyle() == Qt.ToolButtonTextBesideIcon


def test_tooltip():
    b = Button(tooltip='hello')
    assert b.text() == ''
    assert b.icon().isNull()
    assert b.toolTip() == 'hello'
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly

[(10, 'msl.qt.Button', 'Button', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (18, 'msl.qt.QtCore.QSize', 'QtCore.QSize', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (21, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (26, 'msl.qt.Button', 'Button', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (32, 'msl.qt.Button', 'Button', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (47, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (98, 'msl.qt.Button', 'Button', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (103, 'msl.qt.Button', 'Button', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (110, 'msl.qt.Button', 'Button', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (17, 'os.path.dirname', 'os.path.dirname', False, 'import os\n'), (62, 'msl.qt.QtCore.QSize', 'QtCore.QSize', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (69, 'msl.qt.QtCore.QSize', 'QtCore.QSize', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (80, 'msl.qt.QtCore.QSize', 'QtCore.QSize', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (87, 'msl.qt.QtCore.QSize', 'QtCore.QSize', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (90, 'msl.qt.QtCore.QSize', 'QtCore.QSize', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (56, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (61, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (64, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (72, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (79, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (82, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (86, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (89, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (89, 'msl.qt.QtCore.QSize', 'QtCore.QSize', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (93, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (38, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (68, 'msl.qt.QtCore.QSize', 'QtCore.QSize', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (73, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), (94, 'msl.qt.convert.to_qicon', 'convert.to_qicon', False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n')]