#!/usr/bin/python3
"""Apresentação Atlas com dados locais e créditos acessíveis."""
from pathlib import Path
import platform
import subprocess
import sys
from PySide6.QtCore import Qt, QUrl, QTimer
from PySide6.QtGui import QDesktopServices, QIcon, QPixmap
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QFormLayout


def main():
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    app.setApplicationName('Atlas')
    app.setDesktopFileName('atlas-about')
    window = QWidget()
    window.setObjectName('AtlasAbout')
    window.setAttribute(Qt.WA_StyledBackground, True)
    window.setWindowTitle('Sobre a Atlas')
    window.setWindowIcon(QIcon('/usr/share/atlas/atlas.svg'))
    window.resize(620, 560)
    window.setStyleSheet('QWidget {color:palette(window-text);font:14px "Inter";} QWidget#AtlasAbout {background:palette(window);} QLabel {background:transparent;} QPushButton {background:palette(button);border:1px solid palette(mid);border-radius:8px;padding:12px;} QPushButton:hover {background:palette(highlight);} QPushButton:focus {border:2px solid palette(highlight);}')
    layout = QVBoxLayout(window)
    layout.setContentsMargins(36, 30, 36, 30)
    layout.setSpacing(20)
    mark = QLabel()
    mark.setPixmap(QPixmap('/usr/share/atlas/atlas.svg').scaled(88, 88, Qt.KeepAspectRatio, Qt.SmoothTransformation))
    layout.addWidget(mark)
    title = QLabel('Atlas')
    title.setStyleSheet('font-size:36px;font-weight:300;')
    layout.addWidget(title)
    layout.addWidget(QLabel('Seu espaço para criar, organizar e desenvolver.'))
    form = QFormLayout()
    form.setSpacing(12)
    def row(label, text):
        value = QLabel(text)
        value.setTextFormat(Qt.PlainText)
        value.setWordWrap(True)
        value.setTextInteractionFlags(Qt.TextSelectableByMouse)
        form.addRow(label, value)
    release = Path('/usr/share/atlas/release')
    row('Sistema', release.read_text().splitlines()[0] if release.exists() else 'Atlas Linux')
    row('Revisão Atlas', Path('/usr/share/doc/atlas-branding/version').read_text().strip())
    row('Processador', next((line.split(':', 1)[1].strip() for line in Path('/proc/cpuinfo').read_text().splitlines() if line.startswith('model name')), platform.machine()))
    memory = next(line for line in Path('/proc/meminfo').read_text().splitlines() if line.startswith('MemTotal:'))
    row('Memória', f'{int(memory.split()[1]) / 1048576:.1f} GiB')
    row('Kernel', platform.release())
    layout.addLayout(form)
    layout.addStretch()
    actions = QHBoxLayout()
    for label, path in [('Atualizações', 'atlas-updates'), ('Informações técnicas', 'kinfocenter')]:
        button = QPushButton(label)
        button.clicked.connect(lambda checked=False, command=path: subprocess.Popen([command]))
        actions.addWidget(button)
    layout.addLayout(actions)
    credits = QPushButton('Créditos e licenças dos componentes')
    credits.clicked.connect(lambda: QDesktopServices.openUrl(QUrl.fromLocalFile('/usr/share/atlas/about.html')))
    layout.addWidget(credits)
    window.show()
    if '--preview' in sys.argv:
        QTimer.singleShot(600, lambda: (window.grab().save(sys.argv[sys.argv.index('--preview') + 1]), app.quit()))
    sys.exit(app.exec())


if __name__ == '__main__':
    main()
