def auto_detection():
    logging.info('Calling Auto Detection Model for uploaded auto image')
    location = None

    #claim_id=request.form['claim_id']
    #response = send_from_directory("C:\\Users\\shaur\\AppData\\Local\\Programs\\Python\\Python36\\Scripts\\instance\\json", "data_classification.json")
    #with open("data_classification.json") as j:
    #   data = json.load(j)
    #   DigitalBrainModel("").store_auto_detection_model(claim_id, json.dumps(data))

    #Return image as response
    #return response
    claim_id = request.form['claim_id']

    auto_detection_data = DigitalBrainModel("").get_auto_detection_data(
        claim_id)

    if auto_detection_data is None:
        if 'location' in request.args:
            logging.info('Get image location path and image file name ' +
                         request.args['location'])
            location, tail = os.path.split(request.args['location'])
        else:
            logging.info('Failed to image location path and image file name')
            return "Error: No location field provided. Please specify location."

        location = location + '/'

        try:
            logging.info('Delete if json and output folders already exists')
            if (os.path.exists(location + 'json')) and (
                    os.path.exists(location + 'Output')):
                shutil.rmtree(location + 'json')
                shutil.rmtree(location + 'Output')
                logging.info('Deleted json and Output folders in the path ' +
                             location)
        except:
            logging.info(
                'Error to delete json and Output folders in the path ' +
                location)

        #Call Auto Detection Function to detect if any damages
        auto_detect_function(location)

        logging.info('Return json data as response after processing image')
        if os.path.exists(location + 'json'):
            with open(location + 'json/data.json') as f:
                auto_detection_data = json.load(f)

            DigitalBrainModel("").store_auto_detection_model(
                claim_id, str(auto_detection_data))
            return auto_detection_data
        else:
            logging.info('Failed to process image in Auto Detection model')
            return "Auto Detection Process Failed. Check for Logs"
    else:
        return auto_detection_data
def get_amount_by_customer():
    logging.info("Return customer's dependents details")
    policy_id = request.form['policy_id']
    #policy_id="1.23E+08"
    claim_id = request.form['claim_id']
    #claimid = "CL1586954783051"
    return DigitalBrainModel("").get_amount_by_customer(policy_id, claim_id)
def update_status():
    logging.info(
        "This update_status api to update status based agent response")
    claimid = request.form['claimid']
    status = request.form['status']
    isfraud = request.form['is_fraud']
    DigitalBrainModel("").update_status(claimid, status, isfraud)

    return jsonify({'Status Updated': 'true'}), 201
def get_customer_address():
    policy_id=""
    claim_id=""
    try:
        policy_id=request.form['policy_id']
    except:
        logging.info('Failed to capture parameters.')
    try:
        claim_id=request.form['claim_id']
    except:
        logging.info('Failed to capture parameters.')
    address={}
    if policy_id is not "":
        #policy_id="428-59-7371"
        address=DigitalBrainModel("").get_customer_address(policy_id)
    
    elif claim_id is not "":
        address=DigitalBrainModel("").get_customer_address_from_claimid(claim_id)
    print(address)
    return address
def nearby_events():
    policy_id = request.form['policy_id']

    geolocator = Nominatim()
    #customer_id="428-59-7371"
    address = DigitalBrainModel("").get_customer_address(policy_id)
    logging.info("Test")
    location = geolocator.geocode(address["mailing_address_line_1"] + "," +
                                  address["mailing_address_line_2"] + "," +
                                  address["mailing_state"] + "," +
                                  address["mailing_country"],
                                  timeout=30)
    user_location = (location.latitude, location.longitude)
    print("lat long customer: " + str(location.latitude) + "!!!!!!!!!!!!!!" +
          str(location.longitude))
    ret = DigitalBrainModel("").get_city_events("")
    res = []
    print(str(res))
    top = 2
    #ensuring one weather event
    for r in ret:
        event_location = (r["lat"], r["long"])
        #dist=distance_between_points(r["lat"], r["long"], location.latitude, location.longitude)
        dist = geodesic(event_location, user_location).miles
        if (dist <= 3960 and r["category"] == "severe-weather"):
            r["distance"] = int(dist)
            print(dist)
            res += [r]
            break
    #any other event with highest rank
    for r in ret:
        event_location = (r["lat"], r["long"])
        #dist=distance_between_points(r["lat"], r["long"], location.latitude, location.longitude)
        dist = geodesic(event_location, user_location).miles
        if (dist <= 3960 and r["category"] != "severe-weather"):
            r["distance"] = int(dist)
            print(dist)
            res += [r]
            break
    print("THIS IS EVENTS" + str(res))
    return str(res)
def user_login():
    logging.info("This user_login api called to check for authentication for agent")

    username=request.form['username']
    password=request.form['password']
    
    response=DigitalBrainModel("").user_login(username)
    
    if (response == "wrong_username"):
        return jsonify({'status':'false','code':'wrong_username'}),501
    
    if (password==response["password"]):
        return jsonify({'status':'true','user_name':response["customer_name"],"policy_id":response["policy_id"]}),200
    else:
        return jsonify({'status':'false','code':'wrong_password'}),501
def dummy():
    logging.info("dummy details")
    return DigitalBrainModel("").dummy()
def change_password():
    username = request.form['username']
    new_password = request.form['new_password']
    response = DigitalBrainModel("").change_password(username, new_password)
    return response
def get_roadside_assistance():
    logging.info("Return workshop details")
    #customer_id=request.form['customer_id']
    #customer_id="428-59-7371"
    return DigitalBrainModel("").get_roadside_assistance()
def get_dependent_details():
    logging.info("Return customer's dependents details")
    policy_id = request.form['policy_id']
    #customer_id="428-59-7371"
    return DigitalBrainModel("").get_dependent_details(policy_id)
def get_policy_details():
    logging.info("Return policy details")
    policy_id = request.form['policy_id']
    #policy_id="1.23E+08"
    return DigitalBrainModel("").get_policy_details(policy_id)
def add_claim():

    logging.info('Service called to add claim')
    if (request.method == 'POST'):
        logging.info('Get Claim Request')
        #Get claim request as json from POST method
        claim_request = request.form['json']

        no_of_images = request.form['no_of_images']
        no_of_images = int(no_of_images)

        auto_image_file = []
        #Get Auto image path
        for i in range(no_of_images):
            auto_image_file.append(request.files["image_incident_proof_" +
                                                 str(i)])
            #auto_image_file+= [request.files["image_incident_proof_" + str(i)]

        # auto_image_file = request.files['image']
        #Load claim request
        claim_request_json = json.loads(claim_request)

        claim_id = claim_request_json['claimid']
        policy_id = request.form['policy_id']
        uploads_dir = os.path.join(app.instance_path, 'uploads/', claim_id)
        #uploads_dir = uploads_dir + '/' + claim_id
        if not os.path.exists(uploads_dir):
            os.makedirs(uploads_dir)

        logging.info('Save auto image in the path ' + uploads_dir)

        #Save auto image in the path
        for i in range(no_of_images):
            auto_image_file[i].save(
                os.path.join(uploads_dir, auto_image_file[i].filename))

        #Get image path
        # claim_request_json["path"] = os.path.join(uploads_dir, auto_image_file.filename)

        list_of_paths = []

        for i in range(no_of_images):
            list_of_paths.append(
                os.path.join(uploads_dir, auto_image_file[i].filename))

        m = json.dumps(list_of_paths)

        claim_request_json["path"] = m

        logging.info('Add claim request entry into Claim node in Neo4J')
        #Create property for claim request under Claim node in Neo4J
        DigitalBrainModel(claim_request_json).add_claim(policy_id)
        logging.info('Added claim request successfully')

        #Get image path
        # for i in range(no_of_images):
        #     image_path = os.path.join(uploads_dir, auto_image_file[i].filename)

        logging.info('Return image path as response from add_claim request')
        # Return image path
        return jsonify({'Claim Submitted': 'true'}), 200  #image_path #res
    else:
        logging.info('Failed to process claim request. Check for logs')
        return jsonify({'claim Received': 'false'}), 501
def get_policy_id():
    claim_id = request.form['claim_id']
    policy_id = DigitalBrainModel("").get_policy_id(claim_id)
    return policy_id
def retrieve_claim_request():
    logging.info("Return all claim requests")
    return DigitalBrainModel("").retrieve_claim_request()