from __future__ import unicode_literals, print_function
import os
from os import path
import sys
import json

def run_update_oger_file():

    found_new_protein=False
    found_new_cp=False
    found_new_complex=False

    #for oger entity
    if path.exists('glygen_set_oger_entity.json'):
        oger_json_file='glygen_set_oger_entity.json'
        with open(oger_json_file) as jfile:
            oger_dic=json.load(jfile)
    else:
        oger_json_file='glygen_set_oger_entity.json'
        with open(oger_json_file,'w') as jfile:
            json.dump({},jfile)
        oger_dic={}

    if path.exists('glygen_set_oger_entity_cp.json'):
        oger_json_file_cp='glygen_set_oger_entity_cp.json'
        with open(oger_json_file_cp) as jfile:
            oger_cp_dic=json.load(jfile)
    else:
        oger_json_file_cp='glygen_set_oger_entity_cp.json'
        with open(oger_json_file_cp,'w') as jfile:
            json.dump({},jfile)
        oger_cp_dic={}

    if path.exists('glygen_set_oger_entity_complex.json'):
        oger_json_file_complex='glygen_set_oger_entity_complex.json'
        with open(oger_json_file_complex) as jfile:
            oger_complex_dic=json.load(jfile)
    else:
        oger_json_file_complex='glygen_set_oger_entity_complex.json'
        with open(oger_json_file_complex,'w') as jfile:
            json.dump({},jfile)
        oger_complex_dic={}

    #now add the new entities
    if path.exists('oger_entity_tmp.json'):
        oger_entity_tmp_file='oger_entity_tmp.json'
        with open(oger_entity_tmp_file) as jfile:
            oger_entity_tmp_dic=json.load(jfile)

        for ki in oger_entity_tmp_dic.keys():
            if ki not in oger_dic:
                found_new_protein=True
                oger_dic[ki]=oger_entity_tmp_dic[ki]


    if path.exists('oger_entity_cp_tmp.json'):
        oger_entity_cp_tmp_file='oger_entity_cp_tmp.json'
        with open(oger_entity_cp_tmp_file) as jfile:
            oger_entity_cp_tmp_dic=json.load(jfile)

        for ki in oger_entity_cp_tmp_dic.keys():
            if ki not in oger_cp_dic:
                found_new_cp=True
                oger_cp_dic[ki]=oger_entity_cp_tmp_dic[ki]

    if path.exists('oger_entity_complex_tmp.json'):
        oger_entity_complex_tmp_file='oger_entity_complex_tmp.json'
        with open(oger_entity_complex_tmp_file) as jfile:
            oger_entity_complex_tmp_dic=json.load(jfile)

        for ki in oger_entity_complex_tmp_dic.keys():
            if ki not in oger_complex_dic:
                found_new_complex=True
                oger_complex_dic[ki]=oger_entity_complex_tmp_dic[ki]


    if found_new_protein:
        print("Updating OGER Dictionary!")
        with open(oger_json_file,'w') as jfile:
            json.dump(oger_dic,jfile)

    if found_new_cp:
        with open(oger_json_file_cp,'w') as jfile:
            json.dump(oger_cp_dic,jfile)

    if found_new_complex:
        with open(oger_json_file_complex,'w') as jfile:
            json.dump(oger_complex_dic,jfile)