class MunicipalityTableTemplateBuilder:
    municipalityDataAccessor = None
    fileHandler = None
    munID = None

    def __init__(self, munID):
        self.municipalityDataAccessor = MunicipalityDataAccessor(munID)
        self.fileHandler = FileHandler()
        self.munID = munID

    def buildSecondaryTemplates(self):
        def addUnsortColumn(dataType, columnName):
            if MunicipalityDataConfig.municipalityDataSelectiveSortingEnabled:
                if columnName in MunicipalityDataConfig.municipalityDataSortableColumns[dataType]:
                    return ''
                else:
                    return 'class="unsortable"'
            else:
                return ''

        municipalitySecondaryData = self.municipalityDataAccessor.getMunSecondaryData()
        for dataType in municipalitySecondaryData:
            wikiText = '{| class="wikitable sortable" width="95%"\n'
            try:
                numberOfColumns = len(municipalitySecondaryData[dataType][0])
            except IndexError:
                numberOfColumns = 0
            wikiText += '!colspan="' + str(numberOfColumns) + '"|' + MunicipalityDataConfig.munDataProperFinalTableTopics[dataType] + '\n|-\n'
            for column in MunicipalityDataConfig.munDataProperColumnNames[dataType]:
                wikiText += '!scope="col" ' + addUnsortColumn(dataType, column) + '| '+column + '\n'

            for row in municipalitySecondaryData[dataType]:
                wikiText += '|-\n'
                item_count = 0
                for item in row:
                    if item =='NULL':
                        item = ''
                    if item_count == 0:
                        item = '[[' + str(item) + ']]'
                    wikiText += '| ' + str(item) + '\n'
                    item_count += 1
                wikiText += '|-\n'
            wikiText += '|}'
            templateFile = self.fileHandler.writeWikiSourceFile(FileConfig.templateFilePath, self.munID+'_'+dataType+'_Template.txt', 'Template:'+self.munID+'-'+dataType, wikiText)
            PageUploadTableUpdater.addEntry(self.munID, templateFile)

    def buildPrimaryTemplates(self):
        pass

    def buildTableTemplates(self):
        self.buildSecondaryTemplates()
        self.buildPrimaryTemplates()
class OperatorSourceBuilder:
    operatorID = None
    fileHandler = None

    def __init__(self, operatorID):
        self.operatorID = operatorID
        self.fileHandler = FileHandler()

    def buildMainSource(self):
        source = '== Operator Details ==\n'
        source += '{{' + self.operatorID + '-Details}}\n'
        source += '__TOC__\n'
        source += '== Operator Well Data ==\n'
        source += '{{' + self.operatorID + '-wells}}\n'
        source += '__NOEDITSECTION__\n'
        sourceFile = self.fileHandler.writeWikiSourceFile(FileConfig.mainSourcePath, self.operatorID+'_main.txt', self.operatorID, source)
        PageUploadTableUpdater.addEntry(self.operatorID, sourceFile)
class CountySourceBuilder:
    munID = None
    fileHandler = None

    def __init__(self, munID):
        self.munID = munID
        self.fileHandler = FileHandler()

    def buildMainSource(self):
        source = '== Municipality Details ==\n'
        #source += '{{' + self.munID + '-Details}}\n'
        source += '__TOC__\n'
        source += '== Oil and Gas Activity ==\n'
        source += '{{' + self.munID + '-wells}}\n'
        source += '__NOEDITSECTION__\n'
        sourceFile = self.fileHandler.writeWikiSourceFile(FileConfig.mainSourcePath, self.munID+'_main.txt', self.munID, source)
        PageUploadTableUpdater.addEntry(self.munID, sourceFile)
class CountySourceBuilder:
    conID = None
    fileHandler = None

    def __init__(self, conID):
        self.conID = conID
        self.fileHandler = FileHandler()

    def buildMainSource(self):
        source = "== County Details ==\n"
        # source += '{{' + self.conID + '-Details}}\n'
        source += "__TOC__\n"
        source += "== Oil and Gas Activity ==\n"
        source += "{{" + self.conID + "-wells}}\n"
        source += "__NOEDITSECTION__\n"
        sourceFile = self.fileHandler.writeWikiSourceFile(
            FileConfig.mainSourcePath, self.conID + "_main.txt", self.conID, source
        )
        PageUploadTableUpdater.addEntry(self.conID, sourceFile)
class WellTableTemplateBuilder:
    wellDataAccessor = None
    fileHandler = None
    wellID = None

    def __init__(self, wellID):
        self.wellDataAccessor = WellDataAccessor(wellID)
        self.fileHandler = FileHandler()
        self.wellID = wellID

    def buildSecondaryTemplates(self):
        def addUnsortColumn(dataType, columnName):
            if WellDataConfig.wellDataSelectiveSortingEnabled:
                if columnName in WellDataConfig.wellDataSortableColumns[dataType]:
                    return ''
                else:
                    return 'class="unsortable"'
            else:
                return ''

        wellSecondaryData = self.wellDataAccessor.getWellSecondaryData()
        for dataType in wellSecondaryData:
            wikiText = '{| class="wikitable sortable" width="95%"\n'
            try:
                numberOfColumns = len(wellSecondaryData[dataType][0])
            except IndexError:
                numberOfColumns = 0
            wikiText += '!colspan="' + str(numberOfColumns) + '"|' + WellDataConfig.wellDataProperFinalTableTopics[dataType] + '\n|-\n'
            for column in WellDataConfig.wellDataProperColumnNames[dataType]:
                wikiText += '!scope="col" ' + addUnsortColumn(dataType, column) + '| '+column + '\n'

            for row in wellSecondaryData[dataType]:
                wikiText += '|-\n'
                for item in row:
                    wikiText += '| ' + str(item) + '\n'
                wikiText += '|-\n'
            wikiText += '|}'
            templateFile = self.fileHandler.writeWikiSourceFile(FileConfig.templateFilePath, self.wellID+'_'+dataType+'_Template.txt', 'Template:'+self.wellID+'-'+dataType, wikiText)
            PageUploadTableUpdater.addEntry(self.wellID, templateFile)

    def buildPrimaryTemplates(self):
        wellPrimaryData = self.wellDataAccessor.getWellPrimaryData()
        numberOfRows = len(wellPrimaryData)
        wikiText = '{| class="wikitable" width="95%"\n'
        wikiText += '!colspan="2"| Well Details \n'
        wikiText += '|-\n'
        wikiText += '| Well ID: ' + self.wellID + '\n'
        if wellPrimaryData['LATITUDE_DECIMAL'] is not None and wellPrimaryData['LONGITUDE_DECIMAL'] is not None:
            wikiText += '| style="width: 50%; height=100%; align=center" rowspan="' + str(numberOfRows+1) + '" | {{#display_map: ' + str(wellPrimaryData['LATITUDE_DECIMAL'])+', ' + str(wellPrimaryData['LONGITUDE_DECIMAL']) + ' | width=100% | height=100% }} \n'
            wikiText += '|-\n'
        else:
            wikiText += '|-\n'
        if wellPrimaryData['WELL_COUNTY'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['WELL_COUNTY'] + ': [[' + wellPrimaryData['COUNTY_FULL_NAME'] + ']]\n'
            wikiText += '|-\n'
        if wellPrimaryData['WELL_MUNICIPALITY'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['WELL_MUNICIPALITY'] + ': [[' + wellPrimaryData['MUNICIPALITY_FULL_NAME'] + ']]\n'
            wikiText += '|-\n'
        if wellPrimaryData['OPERATOR_NAME'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['OPERATOR_NAME'] + ': [[' + wellPrimaryData['OPERATOR_NAME'] + ']]\n'
            wikiText += '|-\n'
        if wellPrimaryData['OPERATOR_OGO'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['OPERATOR_OGO'] + ': [[' + wellPrimaryData['OPERATOR_OGO'] + ']]\n'
            wikiText += '|-\n'
        if wellPrimaryData['FARM_NAME'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['FARM_NAME'] + ': ' + wellPrimaryData['FARM_NAME'] + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['FIRST_PERMIT_ISSUED_DATE'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['FIRST_PERMIT_ISSUED_DATE'] + ': ' + str(wellPrimaryData['FIRST_PERMIT_ISSUED_DATE']) + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['LAST_PERMIT_ISSUED_DATE'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['LAST_PERMIT_ISSUED_DATE'] + ': ' + str(wellPrimaryData['LAST_PERMIT_ISSUED_DATE']) + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['SPUD_DATE'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['SPUD_DATE'] + ': ' + str(wellPrimaryData['SPUD_DATE']) + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['UNCONVENTIONAL'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['UNCONVENTIONAL'] + ': ' + ('Yes' if wellPrimaryData['UNCONVENTIONAL'] is 'Y' else 'No') + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['HORIZONTAL_WELL'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['HORIZONTAL_WELL'] + ': ' + ('Yes' if wellPrimaryData['HORIZONTAL_WELL'] is 'Y' else 'No') + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['WELL_STATUS'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['WELL_STATUS'] + ': ' + ('Yes' if wellPrimaryData['WELL_STATUS'] == 'Active' else 'No') + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['VIOLATION_COUNT'] != 'NULL':
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['VIOLATION_COUNT'] + ': ' + str(wellPrimaryData['VIOLATION_COUNT']) + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['LATITUDE_DECIMAL'] is not None:
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['LATITUDE_DECIMAL'] + ': ' + str(wellPrimaryData['LATITUDE_DECIMAL']) + '\n'
            wikiText += '|-\n'
        if wellPrimaryData['LONGITUDE_DECIMAL'] is not None:
            wikiText += '| ' + WellDataConfig.wellDetailsDataProperColumnNames['LONGITUDE_DECIMAL'] + ': ' + str(wellPrimaryData['LONGITUDE_DECIMAL']) + '\n'
            wikiText += '|-\n'
        wikiText += '|}'

        templateFile = self.fileHandler.writeWikiSourceFile(FileConfig.templateFilePath, self.wellID+'_Details_Template.txt', 'Template:'+self.wellID+'-Details', wikiText)
        PageUploadTableUpdater.addEntry(self.wellID, templateFile)

    def buildTableTemplates(self):
        self.buildSecondaryTemplates()
        self.buildPrimaryTemplates()
class CountyTableTemplateBuilder:
    CountyDataAccessor = None
    fileHandler = None
    conID = None

    def __init__(self, conID):
        self.CountyDataAccessor = CountyDataAccessor(conID)
        self.fileHandler = FileHandler()
        self.conID = conID

    def buildSecondaryTemplates(self):
        def addUnsortColumn(dataType, columnName):
            if CountyDataConfig.countyDataSelectiveSortingEnabled:
                if columnName in CountyDataConfig.countyDataSortableColumns[dataType]:
                    return ''
                else:
                    return 'class="unsortable"'
            else:
                return ''

        CountySecondaryData = self.CountyDataAccessor.getConSecondaryData()
        for dataType in CountySecondaryData:
            wikiText = '{| class="wikitable sortable" width="95%"\n'
            try:
                numberOfColumns = len(CountyDataConfig.conDataProperColumnNames)
            except IndexError:
                numberOfColumns = 0
            wikiText += '!colspan="' + str(numberOfColumns) + '"|' + CountyDataConfig.conDataProperFinalTableTopics[dataType] + '\n|-\n'
            for column in CountyDataConfig.conDataProperColumnNames[dataType]:
                wikiText += '!scope="col" ' + addUnsortColumn(dataType, column) + '| '+column + '\n'

            if dataType == 'wells':
                for row in CountySecondaryData[dataType]:
                    wikiText += '|-\n'

                    wikiText += '| ' + str('[[' + row + ']]') + '\n'

                    if CountySecondaryData[dataType][row]['WELL_MUNICIPALITY'] != 'NULL':
                        wikiText += '| ' + str('[[' + CountySecondaryData[dataType][row]['MUNICIPALITY_FULL_NAME'] + ']]') + '\n'
                    else:
                        wikiText += '| ' + '\n'

                    if CountySecondaryData[dataType][row]['OPERATOR_NAME'] != 'NULL':
                        wikiText += '| ' + str('[[' + CountySecondaryData[dataType][row]['OPERATOR_NAME'] + ']]') + '\n'
                    else:
                        wikiText += '| ' + '\n'

                    if CountySecondaryData[dataType][row]['FIRST_PERMIT_ISSUED_DATE'] != 'NULL':
                        wikiText += '| ' + str(CountySecondaryData[dataType][row]['FIRST_PERMIT_ISSUED_DATE']) + '\n'
                    else:
                        wikiText += '| ' + '\n'

                    if CountySecondaryData[dataType][row]['SPUD_DATE'] != 'NULL':
                        wikiText += '| ' + str(CountySecondaryData[dataType][row]['SPUD_DATE']) + '\n'
                    else:
                        wikiText += '| ' + '\n'

                    if CountySecondaryData[dataType][row]['UNCONVENTIONAL'] != 'NULL':
                        wikiText += '| ' + ('Yes' if CountySecondaryData[dataType][row]['UNCONVENTIONAL'] is 'Y' else 'No') + '\n'
                    else:
                        wikiText += '| ' + '\n'

                    if CountySecondaryData[dataType][row]['HORIZONTAL_WELL'] != 'NULL':
                        wikiText += '| ' + ('Yes' if CountySecondaryData[dataType][row]['HORIZONTAL_WELL'] is 'Y' else 'No') + '\n'
                    else:
                        wikiText += '| ' + '\n'

                    if CountySecondaryData[dataType][row]['WELL_STATUS'] != 'NULL':
                        wikiText += '| ' + ('Yes' if CountySecondaryData[dataType][row]['WELL_STATUS'] is 'Y' else 'No') + '\n'
                    else:
                        wikiText += '| ' + '\n'

                    if CountySecondaryData[dataType][row]['VIOLATION_COUNT'] != 'NULL':
                        wikiText += '| ' + str(CountySecondaryData[dataType][row]['VIOLATION_COUNT']) + '\n'
                    else:
                        wikiText += '| ' + '\n'

                    wikiText += '|-\n'
            wikiText += '|}'
            templateFile = self.fileHandler.writeWikiSourceFile(FileConfig.templateFilePath, self.conID+'_'+dataType+'_Template.txt', 'Template:'+self.conID+'-'+dataType, wikiText)
            PageUploadTableUpdater.addEntry(self.conID, templateFile)

    def buildPrimaryTemplates(self):
        pass

    def buildTableTemplates(self):
        self.buildSecondaryTemplates()
        self.buildPrimaryTemplates()