#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PySide2.QtWidgets import QWidget, QVBoxLayout, QTabWidget, QHBoxLayout
from PySide2.QtCore import QMargins
from rxbus.core import RxBus
from .DataClass import FileInfo
from rx import operators as ops, scheduler
from .MkEdit import MkEdit
from .MenusBar import MemusBar


class EditCenter(QWidget):
    def __init__(self, parent=None):
        print("EditCenter __init__")

        QWidget.__init__(self, parent)
        self._initUI()
        self._initData()
        self._initEvent()
        self.tabDatas = set()

    def __del__(self):
        print("EditCenter __del__")

    def _initEvent(self):
        self.getMenusBar.openFileCallBack = self.openFileCallBack

    def openFileCallBack(self, value):
        self.openFileInfo(value)

    def openFileInfo(self, value: FileInfo):
        if value.filePath in self.tabDatas:
            print("%s文件已打开" % value.filePath)
        else:
            self.tabDatas.add(value.filePath)
            edit = MkEdit(self)
            edit.loadData(value.filePath)
            edit.setProperty("title", value.filePath)
            self.mdiare.addTab(edit, value.fileName)
            self.mdiare.setTabToolTip(self.mdiare.count() - 1, value.filePath)
            self.mdiare.setCurrentIndex(self.mdiare.count() - 1)

    def tabCloseRequested(self, index):
        _currentTab: MkEdit = self.mdiare.widget(index)
        if not _currentTab.interceptClose():
            self.tabDatas.remove(_currentTab.property("title"))
            self.mdiare.removeTab(index)

    def tabBarClicked(self, index):
        print("tabBarClicked index : %s" % index)

    def tabBarDoubleClicked(self, index):
        print("tabBarDoubleClicked index : %s" % index)

    def currentChanged(self, index):
        print("currentChanged index : %s" % index)

    def destroy(self, destroyWindow: bool = ..., destroySubWindows: bool = ...):
        print("destroy")
        QWidget(EditCenter, self).destroy(destroyWindow, destroySubWindows)

    def create(self, arg__1: int = ..., initializeWindow: bool = ..., destroyOldWindow: bool = ...):
        print("create")
        QWidget(EditCenter, self).create(arg__1, initializeWindow, destroyOldWindow)

    def _initUI(self):
        self.mainLayout = QHBoxLayout(self)
        self.mainLayout.setContentsMargins(QMargins(0, 0, 0, 0))
        self.menuBar = MemusBar(self)
        self.mainLayout.addWidget(self.menuBar)
        self.rightLayout = QVBoxLayout(self)
        self.rightLayout.setContentsMargins(QMargins(0, 0, 0, 0))
        self.mdiare = QTabWidget(self)
        self.mdiare.setDocumentMode(True)
        # 注册关闭按钮信号
        self.mdiare.tabCloseRequested.connect(self.tabCloseRequested)
        self.mdiare.tabBarClicked.connect(self.tabBarClicked)
        self.mdiare.tabBarDoubleClicked.connect(self.tabBarDoubleClicked)
        self.mdiare.currentChanged.connect(self.currentChanged)
        self.mdiare.setMovable(True)
        self.mdiare.setTabsClosable(True)
        self.rightLayout.addWidget(self.mdiare)
        self.mainLayout.addLayout(self.rightLayout, 1)
        self.setLayout(self.mainLayout)

    def _initData(self):
        pass
        # edit = Edit(self)
        # self.mdiare.addTab(edit, "xxxx")
        # for i in range(10):

    @property
    def getMenusBar(self):
        return self.menuBar
