import matplotlib
# Allow running headless from the command line
matplotlib.use("agg")

import os
import datetime
import json
import glob
import re
import chatbot

from pylab import *
import numpy as np
import mutils as m

space = 'AAAAte3wnhw'
if( ("CHAT_API_TOKEN" in os.environ) and ("CHAT_API_KEY" in os.environ)):
  token = os.environ["CHAT_API_TOKEN"]
  key = os.environ["CHAT_API_KEY"]
else:
  exit()

#key   ='AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI'
#token ='BkqsF7agVwmha7J0patsuPEGkh14Cm1G568Ue2DQsdQ%3D'
#space = 'AAAAarQBZ0g'

# Override pylab find() to avoid deprecation warning
# pylint: disable=function-redefined
def find(x):
  return where(x)[0]

def natural_key(string_):
  """See http://www.codinghorror.com/blog/archives/001018.html"""
  return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_)]


def processFile(filename,plotname,year,month,day,orbit,satStr='',sigStr='',summary=True):
  string = None
  if(os.path.isfile(filename) and (os.path.getsize(filename) > 0)):
    slipData = np.loadtxt(filename,dtype='int')
    rows = size(slipData)/8
    if(rows > 1):
      DOY = np.array([(datetime.datetime(*x.astype(int)) - datetime.datetime(2018,1,1)).days for x in slipData[:,0:3]])
      numSlips  = slipData[:,6]
      numEpochs = slipData[:,7]
      numEntries = len(DOY) - 1

      thisSlips   = numSlips[numEntries].astype(float)
      thisEpochs  = numEpochs[numEntries].astype(float)

      if(summary == True):
        if(orbit == 1): # BDS GEO
          dThreshold = 5000 # PPM (0.5%)
          dEpochThreshold = 100000
        else:
          dThreshold = 1500 # PPM (0.15%)
          dEpochThreshold = 100000
      else:
        if(orbit == 1): # BDS GEO
          dThreshold = 10000 # PPM (1%)
          dEpochThreshold = 10000
        else:
          dThreshold = 8000 # PPM (0.8%)
          dEpochThreshold = 10000

      if( (yesterdayDayNum == DOY[numEntries].astype(int)) and (thisEpochs > dEpochThreshold) ):
        # Convert to PPM 
        rate = 1e6 * thisSlips / thisEpochs

        if(rate > dThreshold):
          if(summary == True):
            report = "%4d%02d%02d *Summary* Slip Rate Failure %s %s %s PPM=%.0f Slips=%.0f Epochs=%.0f\n" % (year,month,day,LocalDir,satTypeStr,thisSat['signals'][sigType]['sigStr'],rate,thisSlips,thisEpochs)
            token = filename.split('/')
            report += "http://quark.eng.trimble.com/DataDump/Slips/" + token[0] + "/" + satStr + '-' + sigStr + ".png\n"
          else:
            indexStart = filename.find('SV') + 2
            indexEnd   = filename.find('.txt')
            SVID = int(filename[indexStart:indexEnd])
            report = "%4d%02d%02d Slip Rate Failure %s %s SV%d %s PPM=%.0f Slips=%.0f Epochs=%.0f\n" % (year,month,day,LocalDir,satTypeStr,SVID,thisSat['signals'][sigType]['sigStr'],rate,thisSlips,thisEpochs)

          report += "http://quark.eng.trimble.com/DataDump/Slips/%s\n" % filename
          report += "http://quark.eng.trimble.com/DataDump/Slips/%s\n" % plotname


          # This is a GNSS test room
          #thread=str(year) + str(month).zfill(2) + str(day).zfill(2)
          #chatbot.createMessage(key,token,space,report,thread=thread)
          string = report 
          print(report,end='')

  return(string)



systems = 20
freqs   = 20
signals = 50
svType  = 2

plotSymb = [ "bo", "ro", "co", "go", "ko", "yo", "mo",
             "b*", "r*", "c*", "g*", "k*", "y*", "m*"]

# Load the receiver JSON file 
with open('receivers.json', 'r') as f:
  receivers = json.load(f)
MaxRX = len(receivers['receivers'])

# Load the signal LUT JSON file 
with open('signals.json', 'r') as f:
  signals = json.load(f)
MaxSatType = len(signals['types'])

now = datetime.datetime.utcnow()
yesterday = now - datetime.timedelta(days=2) # To match companion scripts
yesterdayDayNum = (yesterday - datetime.datetime(2018,1,1)).days

year  = yesterday.year
month = yesterday.month
day   = yesterday.day

print(year,month,day)

numErr = 0
#for elevType in range(2):
if(True):
  elevType = 0
  if(elevType == 0):
    modifier = ''
  else:
    modifier = '30'

  for rx in range(MaxRX):
    rxString = ''
    LocalDir = receivers['receivers'][rx]['RXStr']
    rxName   = receivers['receivers'][rx]['rxName']
    shortName = receivers['receivers'][rx]['rxShortName']

    if(LocalDir.startswith('Sing')):
      continue
    if(LocalDir.startswith('Tokyo')):
      continue
    if(LocalDir.startswith('Melb')):
      continue

    for satType in range(MaxSatType):
      thisSat    = signals['types'][satType]
      satTypeStr = thisSat['satTypeStr']
    
      MaxSig = len(thisSat['signals'])
      for sigType in range(MaxSig):
        baseName = (LocalDir + "/slips" + modifier + "." + 
                    thisSat['signals'][sigType]['satType'] + "." + 
                    thisSat['signals'][sigType]['freq'] + "." + 
                    thisSat['signals'][sigType]['sig'] + "." + 
                    thisSat['signals'][sigType]['orbit'])
        filename =  baseName + '.txt'


        plotname = ('History/' + shortName + "-slips" + modifier + "." + 
                    thisSat['signals'][sigType]['satType'] + "." + 
                    thisSat['signals'][sigType]['freq'] + "." + 
                    thisSat['signals'][sigType]['sig'] + '.png')



        string = processFile(filename,plotname,year,month,day,int(thisSat['signals'][sigType]['orbit']),
                             satStr=satTypeStr,
                             sigStr=thisSat['signals'][sigType]['plotStr'])
        if(string is not None):
          rxString += string
          numErr+=1

        # Now perform a per SV test 
        fileList = glob.glob(baseName + '.SV*.txt')
        # Perform a natural sort as we have mixed string/numbers
        fileList = sorted(fileList,key=natural_key)
        for i in range(len(fileList)):
          string = processFile(fileList[i],plotname,year,month,day,int(thisSat['signals'][sigType]['orbit']),summary=False)
          if(string is not None):
            rxString += string
            numErr+=1

    if(rxString is not ''):
      print("*%s*" % rxString)
      thread=str(year) + str(month).zfill(2) + str(day).zfill(2)
      # Chatbot fails if the buffer is greater than 4096 bytes
      lines = rxString.splitlines(keepends=True)
      newBuffer = ''
      for i in range(len(lines)):
        newBuffer += lines[i]
        if(len(newBuffer) > 3900):
          chatbot.createMessage(key,token,space,newBuffer,thread=thread)
          newBuffer = ''
      if(len(newBuffer) > 0): # Output any remaining data
        chatbot.createMessage(key,token,space,newBuffer,thread=thread)

    else:
      print("No outliers")


if(numErr > 0):
  message = 'Summary Results:\n'
  message += '0 Deg Mask: http://higgs.eng.trimble.com:8080/ReportedSlips.html?elevAngle=0\n'
  message += '30 Deg Mask: http://higgs.eng.trimble.com:8080/ReportedSlips.html?elevAngle=30\n\n'

  thread=str(year) + str(month).zfill(2) + str(day).zfill(2)
  chatbot.createMessage(key,token,space,message,thread=thread)



              
