#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Get all sites
# Copyright (C) 2012-2024 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@simula.no

# NorNet
from NorNetSiteSetup import *
from NorNetTools     import *
from NorNetAPI       import *



# ###### Main program #######################################################
loginToPLC(quietMode = True)

siteList = fetchNorNetSite(None, True)


siteIndexList = []
for siteIndex in siteList:
   siteIndexList.append(siteIndex)

if len(sys.argv) > 1:
   print('{0:3s} {1:1s} {2:8s} {3:36s} {4:4s} {5:25s} {6:25s} {7:9s} {8:10s} {9:3s} {10:12s} {11:42s} {12:20s} {13:36s} {14:10s} {15:10s}'.format(
      'ID', 'A', 'SSN', 'SiteLongName', 'CC', 'Province', 'City', 'Lat', 'Lon', 'ISP', 'PSN', 'ProviderLongName', 'IPv4', 'IPv6', 'Downlink', 'Uplink'
   ))

for siteIndex in sorted(siteIndexList):
   site         = siteList[siteIndex]
   siteName     = site['site_utf8']
   siteEnabled  = site['site_enabled']
   siteTagsList = site['site_tags']
   providerList = getNorNetProvidersForSite(siteList[siteIndex])

   # print(site)

   isp = 1
   for onlyDefault in [ True, False ]:
      for providerIndex in providerList:
         if ( ((onlyDefault == True)  and (providerIndex == site['site_default_provider_index'])) or \
               ((onlyDefault == False) and (providerIndex != site['site_default_provider_index'])) ):
            provider = providerList[providerIndex]
            externalNetworkIPv4 = IPv4Address(str(provider['provider_tunnelbox_ipv4'].ip))
            externalNetworkIPv6 = IPv6Address(str(provider['provider_tunnelbox_ipv6'].ip))
            externalGatewayIPv4 = IPv4Address(str(provider['provider_gateway_ipv4']))
            externalGatewayIPv6 = IPv6Address(str(provider['provider_gateway_ipv6']))

            print('{0:3d} {1:1d} {2:8s} {3:36s} {4:2s} {5:25s} {6:25s} {7:s} {8:s} {9:3d} {10:12s} {11:42s} {12:20s} {13:36s} {14:10s} {15:10s}'.format(
                     int(site['site_index']),
                     int(siteEnabled),
                     '"' + str(site['site_short_name']) + '"',
                     '"' + str(site['site_utf8']) + '"',
                     '"' + getTagValue(siteTagsList, 'nornet_site_country_code', '??' ) + '"',
                     '"' + getTagValue(siteTagsList, 'nornet_site_province', '?')       + '"',
                     '"' + getTagValue(siteTagsList, 'nornet_site_city', '?')           + '"',
                     fill(str(site['site_latitude']), 9),
                     fill(str(site['site_longitude']), 10),
                     isp,
                     '"' + provider['provider_short_name'] + '"',
                     '"' + provider['provider_long_name']  + '"',
                     '"' + str(externalNetworkIPv4) + '"',
                     '"' + str(externalNetworkIPv6) + '"',
                     str(provider['provider_downstream']),
                     str(provider['provider_upstream'])
                  ))

            isp = isp + 1
