import pandas as pd
import tableauscraper
from typing import List


class TableauWorksheet:

    name: str = ""
    data: pd.DataFrame = pd.DataFrame()
    cmdResponse: bool = False

    _originalData = {}
    _originalInfo = {}
    _data_dictionnary = {}
    _scraper = None

    def __init__(
        self,
        scraper,
        originalData,
        originalInfo,
        worksheetName,
        dataFrame,
        cmdResponse=False,
    ):
        self._scraper = scraper
        self.name = worksheetName
        self.data = dataFrame
        self._originalData = originalData
        self._originalInfo = originalInfo
        self.cmdResponse = cmdResponse
        if self.cmdResponse:
            presModel = self._originalData["vqlCmdResponse"]["layoutStatus"][
                "applicationPresModel"
            ]
            self._data_dictionnary = tableauscraper.utils.getDataFullCmdResponse(
                presModel
            )
        else:
            self._data_dictionnary = tableauscraper.utils.getDataFull(
                self._originalData
            )

    def getColumns(self) -> List[str]:
        return [
            t["fieldCaption"]
            for t in tableauscraper.utils.getIndicesInfo(
                self._originalData, self.name, noSelectFilter=True
            )
        ]

    def getSelectableColumns(self) -> List[str]:
        return [
            t["fieldCaption"]
            for t in tableauscraper.utils.getIndicesInfo(
                self._originalData, self.name, noSelectFilter=False
            )
        ]

    def getValues(self, column) -> List[str]:
        columnObj = [
            t
            for t in tableauscraper.utils.getIndicesInfo(
                self._originalData, self.name, noSelectFilter=True
            )
            if t["fieldCaption"] == column
        ]
        if len(columnObj) == 0:
            return []
        frameData = tableauscraper.utils.getData(
            self._originalData, self._data_dictionnary, [columnObj[0]]
        )
        frameDataKeys = list(frameData.keys())

        if len(frameDataKeys) == 0:
            return []
        return frameData[frameDataKeys[0]]

    def select(self, column, value):
        values = self.getValues(column)
        try:
            index = values.index(value)
            r = tableauscraper.api.select(self._scraper, self.name, [index + 1])
            return tableauscraper.dashboard.getWorksheetsCmdResponse(self._scraper, r)
        except ValueError:
            return tableauscraper.TableauDashboard(
                scraper=self._scraper, originalData={}, originalInfo={}, data=[]
            )
