import matplotlib
import numpy as np
import pandas as pd
from funcoes.dados import mse
from PyQt5.QtCore import QObject, QAbstractTableModel, Qt
from PyQt5.QtWidgets import QTableView

class ctrl_func(mse):

    fs: float = 2

    def __init__(self, views):
        super().__init__()

        self._views = views

    def av_ctrl(self):

        self.sondagens = self._views[0].num_son.value()
        pd.set_option('display.max_rows', self.sondagens)

        self.tab = pd.DataFrame(index=range(0, self.sondagens),
                                columns=['Prof_m', 'Nspt', 'F1', 'K_kN/cm²',
                                         'Ap_cm²', 'Pp_kN', 'F2', 'K_kN/cm²',
                                         'α', 'Perim_cm', 'Pl, unit._kN',
                                         'Pl, acum._kN', 'Pr_kN'])
        self.tab['Prof_m'] = range(0, self.sondagens)
        self.tab['Nspt'] = (1, 2, 3, 4)
        self.tab['Nspt'] = self.tab['Nspt'].apply(lambda x: x + 1)

    def av_lst_cpt(self):

        self.cpt_lst = []
        self.cpt_lst_int = []

        for i in range(0, self.sondagens):
            self.cpt_lst.append(self._views[5].cpt_layout.itemAt(i, 1))

        for ele in self.cpt_lst:
            self.cpt_lst_int.append(ele.widget().value())

        print(self.cpt_lst_int)

    def av_conv_cpt(self, txt):

        print('teste')

        self.solo = self.kc_cpt_nspt_av
        self.solo_escolha = txt
        for index, tipo in enumerate(self.solo['Tipo de solo']):
            if tipo == self.solo_escolha:
                self.kc = self.solo.at[int(index), 'Kc_kPa']
        self.nspt_lst = [ele * float(self.kc) for ele in self.cpt_lst_int]
        print(self.nspt_lst)

    def ref_tab_est(self):

        self.reftab = tabelas(self.estaca_mil)
        self.view = QTableView()
        self.header = self.view.horizontalHeader()
        self.header.setDefaultAlignment(Qt.AlignHCenter)
        self.view.setModel(self.reftab)
        self.view.resize(800, 600)
        self.view.show()

class tabelas(QAbstractTableModel):

    def __init__(self, data):
        QAbstractTableModel.__init__(self)

        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parent=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None

import matplotlib
import numpy as np
import pandas as pd
from funcoes.dados import mse
from PyQt5.QtCore import QObject, QAbstractTableModel, Qt
from PyQt5.QtWidgets import QTableView

class ctrl_func(mse):

    fs: float = 2

    def __init__(self, views):
        super().__init__()

        self._views = views

    def av_ctrl(self):

        self.sondagens = self._views[0].num_son.value()
        pd.set_option('display.max_rows', self.sondagens)

        self.tab = pd.DataFrame(index=range(0, self.sondagens),
                                columns=['Prof_m', 'Nspt', 'F1', 'K_kN/cm²',
                                         'Ap_cm²', 'Pp_kN', 'F2', 'K_kN/cm²',
                                         'α', 'Perim_cm', 'Pl, unit._kN',
                                         'Pl, acum._kN', 'Pr_kN'])
        self.tab['Prof_m'] = range(0, self.sondagens)
        self.tab['Nspt'] = (1, 2, 3, 4)
        self.tab['Nspt'] = self.tab['Nspt'].apply(lambda x: x + 1)

    def av_lst_cpt(self):

        self.cpt_lst = []
        self.cpt_lst_int = []

        for i in range(0, self.sondagens):
            self.cpt_lst.append(self._views[5].cpt_layout.itemAt(i, 1))

        for ele in self.cpt_lst:
            self.cpt_lst_int.append(ele.widget().value())

        print(self.cpt_lst_int)

    def av_conv_cpt(self, txt):

        print('teste')

        self.solo = self.kc_cpt_nspt_av
        self.solo_escolha = txt
        for index, tipo in enumerate(self.solo['Tipo de solo']):
            if tipo == self.solo_escolha:
                self.kc = self.solo.at[int(index), 'Kc_kPa']
        self.nspt_lst = [ele * float(self.kc) for ele in self.cpt_lst_int]
        print(self.nspt_lst)

    def ref_tab_est(self):

        self.reftab = tabelas(self.estaca_mil)
        self.view = QTableView()
        self.header = self.view.horizontalHeader()
        self.header.setDefaultAlignment(Qt.AlignHCenter)
        self.view.setModel(self.reftab)
        self.view.resize(800, 600)
        self.view.show()

class tabelas(QAbstractTableModel):

    def __init__(self, data):
        QAbstractTableModel.__init__(self)

        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parent=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None
