from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import (QActionGroup, QComboBox, QFrame, QGroupBox,
                                 QHBoxLayout, QSplitter, QToolButton,
                                 QVBoxLayout)

from .config import toolButtonStyle


def createPatternTab(self):
    self.patInfChoice = QGroupBox('Pattern info')                               # create display widget(s)
    self.patNr1Choice = QGroupBox('1st pattern')                                # create display widget(s)
    self.patNr2Choice = QGroupBox('2nd pattern')                                # create display widget(s)

    self.patInfChoice.setMinimumWidth(140)
    self.patNr1Choice.setMinimumWidth(140)
    self.patNr2Choice.setMinimumWidth(140)

    self.patInfChoice.setAlignment(Qt.AlignmentFlag.AlignHCenter)
    self.patNr1Choice.setAlignment(Qt.AlignmentFlag.AlignHCenter)
    self.patNr2Choice.setAlignment(Qt.AlignmentFlag.AlignHCenter)

    vbox0 = QVBoxLayout()
    vbox0.addStretch(2)                                                         # add some stretch to main center widget(s)
    vbox0.addWidget(self.patInfChoice)                                          # add main widget(s)
    vbox0.addStretch(1)                                                         # add some stretch to main center widget(s)
    vbox0.addWidget(self.patNr1Choice)                                          # add main widget(s)
    vbox0.addStretch(1)                                                         # add some stretch to main center widget(s)
    vbox0.addWidget(self.patNr2Choice)                                          # add main widget(s)
    vbox0.addStretch(9)                                                         # add some stretch to main center widget(s)

    self.tbPatternLayout = QToolButton()
    self.tbPatternKxKy = QToolButton()

    self.tbPatternLayout.setMinimumWidth(110)
    self.tbPatternKxKy.setMinimumWidth(110)

    self.tbPatternLayout.setStyleSheet(toolButtonStyle)
    self.tbPatternKxKy.setStyleSheet(toolButtonStyle)

    self.tbPatternLayout.setDefaultAction(self.actionPatternLayout)             # coupling done here
    self.tbPatternKxKy.setDefaultAction(self.actionPatternKxKy)

    self.patternActionGroup = QActionGroup(self)                                # the QActionGroup provides text label to QToolButtons
    self.patternActionGroup.addAction(self.actionPatternLayout)
    self.patternActionGroup.addAction(self.actionPatternKxKy)
    self.actionPatternLayout.setChecked(True)

    self.actionPatternLayout.triggered.connect(self.onActionPatternLayoutTriggered)
    self.actionPatternKxKy.triggered.connect(self.onActionPatternKxKyTriggered)

    vbox1 = QVBoxLayout()                                                       # vertical layout for analysis options
    vbox1.addWidget(self.tbPatternLayout)
    vbox1.addWidget(self.tbPatternKxKy)
    self.patInfChoice.setLayout(vbox1)

    self.pattern1 = QComboBox()                                                 # vertical layout for selection 1st pattern
    self.pattern1.currentIndexChanged.connect(self.onPattern1IndexChanged)

    vbox2 = QVBoxLayout()
    vbox2.addWidget(self.pattern1)
    self.patNr1Choice.setLayout(vbox2)

    self.pattern2 = QComboBox()                                                 # vertical layout for selection 2nd pattern
    self.pattern2.currentIndexChanged.connect(self.onPattern2IndexChanged)

    vbox3 = QVBoxLayout()
    vbox3.addWidget(self.pattern2)
    self.patNr2Choice.setLayout(vbox3)

    self.pattern1.clear()
    self.pattern1.addItem('<no pattern>')                                       # pattern list
    for item in self.survey.patternList:
        self.pattern1.addItem(item)

    self.pattern2.clear()
    self.pattern2.addItem('<no pattern>')                                       # pattern list
    for item in self.survey.patternList:
        self.pattern2.addItem(item)

    # create widget containers for the overall layout
    hbox0 = QHBoxLayout()                                          # Make the vertical layout centered horizontally
    hbox0.addStretch()                                             # add some stretch to main center widget(s)
    hbox0.addLayout(vbox0)                                         # add the center widget, which uses QVboxLayout
    hbox0.addStretch()                                             # add some stretch to main center widget(s)

    leftSide = QFrame()
    leftSide.setFrameShape(QFrame.Shape.StyledPanel)
    leftSide.setLayout(hbox0)
    leftSide.setMaximumWidth(180)
    rightSide = self.arraysWidget

    splitter1 = QSplitter(Qt.Orientation.Horizontal)
    splitter1.addWidget(leftSide)
    splitter1.addWidget(rightSide)
    splitter1.setSizes([100, 500])

    tabLayout = self.tabPatterns.layout()
    if tabLayout is None:
        tabLayout = QHBoxLayout(self.tabPatterns)
        tabLayout.setContentsMargins(0, 0, 0, 0)
    else:
        while tabLayout.count():
            item = tabLayout.takeAt(0)
            widget = item.widget()
            if widget is not None:
                widget.setParent(None)

    tabLayout.addWidget(splitter1)
