__author__ = 'asifj'
import requests
from pymongo import MongoClient
import json
import csv
import traceback
import logging
from tabulate import tabulate
from bson.json_util import dumps

logging.basicConfig(
    format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
    level=logging.DEBUG
)

class HBase:
    def __init__(self):
        self.url = "http://172.22.147.248:8092/api/"
        pass

    def get_case_by_case_id(self, document, row):
        print "Making request..."
        print self.url+"case-manager/cases/"+str(document['caseId'])
        r = requests.get(self.url+"case-manager/cases/"+str(document['caseId']))
        print "CaseID: "+str(document['caseId'])
        print "Response: "+str(r.status_code)
        keys = len(document.keys())
        print "Keys: "+str(keys)
        row.append(r.status_code)
        if r.status_code==200:
            response = json.loads(r.text)
            table = []
            if not (str(document['caseId']).strip() == "" if response['srId'] is None else str(response['srId']).strip()):
                print "Incorrect value for 'caseId'!"
                status = 1
            print "Document"
            print "========"
            print dumps(document, sort_keys=True)
            print "Response CaseNotes"
            print "=================="
            print dumps(response['caseNotes'][4], sort_keys=True)

            response_notes_len = len(response['caseNotes'])
            document_notes_len = len(document['note'])

            print "No of notes in response: "+str(response_notes_len)
            print "No of notes in document: "+str(document_notes_len)
            found = 0
            for i in range(0, response_notes_len):
                tmp = []
                if document['note']['tdline'] == response['caseNotes'][i]['noteLogMin']:
                    found == 1
                    changeDate = "" if response['caseNotes'][i]['changeDate'] is None else str(response['caseNotes'][i]['changeDate']).strip()
                    changeTime = "" if response['caseNotes'][i]['changeTime'] is None else str(response['caseNotes'][i]['changeTime']).strip()
                    originatorRole = "" if response['caseNotes'][i]['originatorRole'] is None else str(response['caseNotes'][i]['originatorRole']).strip()
                    responsibleGroup = "" if response['caseNotes'][i]['responsibleGroup'] is None else str(response['caseNotes'][i]['responsibleGroup']).strip()
                    countryKey = "" if response['caseNotes'][i]['countryKey'] is None else str(response['caseNotes'][i]['countryKey']).strip()
                    noteType = "" if response['caseNotes'][i]['noteType'] is None else str(response['caseNotes'][i]['noteType']).strip()
                    creationMethod = "" if response['caseNotes'][i]['creationMethod'] is None else str(response['caseNotes'][i]['creationMethod']).strip()
                    originator = "" if response['caseNotes'][i]['originator'] is None else str(response['caseNotes'][i]['originator']).strip()
                    privatePublic = "" if response['caseNotes'][i]['privatePublic'] is None else str(response['caseNotes'][i]['privatePublic']).strip()
                    supervisor = "" if response['caseNotes'][i]['supervisor'] is None else str(response['caseNotes'][i]['supervisor']).strip()
                    theater = "" if response['caseNotes'][i]['theater'] is None else str(response['caseNotes'][i]['theater']).strip()
                    noteName = "" if response['caseNotes'][i]['noteName'] is None else str(response['caseNotes'][i]['noteName']).strip()
                    changeDate = "" if response['caseNotes'][i]['changeDate'] is None else str(response['caseNotes'][i]['changeDate']).strip()

                    if not str(document['udate']).strip() == changeDate:
                        tmp = [i]
                        tmp.append("udate")
                        tmp.append(document['udate'])
                        tmp.append(response['caseNotes'][i]['changeDate'])
                        tmp.append("Failed")
                        table.append(tmp)
                        found = 1
                    if not str(document['utime']).strip() == changeTime:
                        tmp = [i]
                        tmp.append("utime")
                        tmp.append(document['utime'])
                        tmp.append(response['caseNotes'][i]['changeTime'])
                        tmp.append("Failed")
                        table.append(tmp)
                        found = 1
                    if not str(document['zoriginatorrole']).strip() == originatorRole:
                        tmp = [i]
                        tmp.append("zoriginatorrole")
                        tmp.append(document['zoriginatorrole'])
                        tmp.append(response['caseNotes'][i]['originatorRole'])
                        tmp.append("Failed")
                        table.append(tmp)
                        found = 1
                    if not str(document['zrespgroup']).strip() == responsibleGroup:
                        tmp = [i]
                        tmp.append("zrespgroup")
                        tmp.append(document['zrespgroup'])
                        tmp.append(response['caseNotes'][i]['responsibleGroup'])
                        tmp.append("Failed")
                        table.append(tmp)
                        found = 1
                    if not str(document['zcountry']).strip() == countryKey:
                        tmp = [i]
                        tmp.append("zcountry")
                        tmp.append(document['zcountry'])
                        tmp.append(response['caseNotes'][i]['countryKey'])
                        tmp.append("Failed")
                        table.append(tmp)
                        found = 1
                    if not str(document['tdid']).strip() == noteType:
                        tmp = [i]
                        tmp.append("tdid")
                        tmp.append(document['tdid'])
                        tmp.append(response['caseNotes'][i]['noteType'])
                        tmp.append("Failed")
                        table.append(tmp)