#  Copyright (c) 2025  Jeffrey Hsu - JITToolBox
#  #
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  #
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  #
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.

from PySide6.QtCore import Qt
from PySide6.QtGui import QIcon
from qfluentwidgets import InfoBar, InfoBarPosition, InfoBarIcon, FluentIconBase, ProgressBar, IndeterminateProgressBar, \
    InfoBarManager


class ProgressInfoBar(InfoBar):
    def __init__(self, icon: InfoBarIcon | FluentIconBase | QIcon | str = InfoBarIcon.INFORMATION, title: str = "1",
                 content: str = "", orient=Qt.Horizontal,
                 isClosable=True, duration=1000,
                 position=InfoBarPosition.BOTTOM, parent=None):
        super().__init__(icon, title, content, orient, isClosable, duration, position, parent)

        self.pb = ProgressBar()
        self.ipb = IndeterminateProgressBar(start=True)

        self.addWidget(self.pb)
        self.addWidget(self.ipb)

        self.hide_progress()

        self.show()

    def set_progress(self, value: int):
        manager = InfoBarManager.make(self.position)

        if value < 0 or value > 100:
            if not self.ipb.isVisible():
                manager.remove(self)
                self.ipb.setVisible(True)
                self.pb.setVisible(False)
                self.adjustSize()
                manager.add(self)

        else:
            if not self.pb.isVisible():
                manager.remove(self)
                self.pb.setVisible(True)
                self.ipb.setVisible(False)
                self.adjustSize()
                manager.add(self)
            self.pb.setValue(value)

    def hide_progress(self):
        self.pb.setVisible(False)
        self.ipb.setVisible(False)
        self.adjustSize()

    def set_title(self, title: str):
        if title == self.title:
            return
        manager = InfoBarManager.make(self.position)
        manager.remove(self)

        self.title = title
        self._adjustText()

        manager.add(self)

    def show_success(self, title: str = '成功！', content: str = "操作已完成", isCloseable: bool = True,
                     duration: int = 5000):

        self.setVisible(False)
        InfoBarManager.make(self.position).remove(self)

        InfoBar.success(
            title=title,
            content=content,
            orient=Qt.Horizontal,
            isClosable=isCloseable,
            position=InfoBarPosition.BOTTOM,
            duration=duration,
            parent=self.parent()
        )

    def show_error(self, title: str = '错误！', content: str = "操作失败", isCloseable: bool = True,
                   duration: int = 5000):
        self.setVisible(False)
        InfoBarManager.make(self.position).remove(self)

        InfoBar.error(
            title=title,
            content=content,
            orient=Qt.Horizontal,
            isClosable=isCloseable,
            position=InfoBarPosition.BOTTOM,
            duration=duration,
            parent=self.parent()
        )
