def checkAdminLogin():
    passwd=request.json.get("password")
    print(passwd)
    if(passwd=="admin"):
        return generate_response(200,"Admin Logged in.")
    else:
        return generate_response(401,"Authorization Failed.")
def AddBroker():
    print(request)
    firstname = request.json.get("firstname")
    lastname = request.json.get("lastname")
    email = request.json.get("email")

    brokerExists = Broker.query.filter_by(email=email).all()
    if(len(brokerExists)):
        return generate_response(400, "This e-mail address is already registered.")
    address = request.json.get("address")
    print(firstname,lastname,email,address)
    lat, lng = getGeocoding(address)
    email_domain = email.split("@")[1].replace(" ", "").replace("\n", "")
    assined_agency_id = None
    agency = Agency_Domain_WhiteList.query.filter_by(domain=email_domain).first()
    if (agency != None):  # domain in white list
        possibleAgencies = Agency.query.filter_by(domain=email_domain).all()

        if(len(possibleAgencies)==0):  # if domain is in white list but no possible agency.
            db.session.add(Agency(title=email_domain ,domain=email_domain,address=address,lat=lat,lng=lng))
            db.session.commit()
            newCreatedAgency = Agency.query.filter_by(address=address).first()
            db.session.add(
                Broker(agencyId=newCreatedAgency.id, firstname=firstname, lastname=lastname, email=email, address=address,
                       lat=lat, lng=lng))
            db.session.commit()
            return generate_response(200, "There was not any associated agency. A new one was created and connected to the broker.")
        elif (len(possibleAgencies) > 1):  # if more than one agency, then get closest one.
            agencyIndex = getClosestAgency([(agency.lat, agency.lng) for agency in possibleAgencies], (lat, lng))
            assined_agency_id = possibleAgencies[agencyIndex].id
            db.session.add(
                Broker(agencyId=assined_agency_id, firstname=firstname, lastname=lastname, email=email, address=address,
                       lat=lat, lng=lng))
            db.session.commit()

            return generate_response(200,"Broker is added succsessfully and connected to agency that located in \"" + possibleAgencies[agencyIndex].address + "\"")
        else: # choose the only available agency.
            assined_agency_id = possibleAgencies[0].id
            db.session.add(Broker(agencyId=assined_agency_id, firstname=firstname, lastname=lastname, email=email, address=address,lat=lat, lng=lng))
            db.session.commit()
            return generate_response(200,"Broker is added succsessfully and connected to single available agency that located in \"" + possibleAgencies[0].address + "\"")

    else: # broker's email domain address not in whitelist. Still, do not lose this information and save in blacklistbroker table.
        db.session.add(BlackListBrokers(domain=email_domain, firstname=firstname, lastname=lastname, email=email, address=address,lat=lat, lng=lng))
        db.session.commit()
        return generate_response(400, "Broker can not be added. Only white listed agencies can register.")
def listAgencies():
    page = request.args.get("page")
    perpage = request.args.get("per_page")
    ls=None
    totalCount = Agency.query.count()
    if(page != None and perpage !=None):
        ls = Agency.query.offset((int(page)-1)*int(perpage)).limit(int(perpage)).all()
    else:
        ls = Agency.query.all()

    if(len(ls)>0):
        resp=[*map(agencySerializer, ls)]
        return jsonify({"data":resp,"total":totalCount})
    else:
        return generate_response("400", " Invalid Payload ")
def bad_request(e):
    return generate_response(400, 'Bad request.')
def not_found(e):
    return generate_response(404, 'Resource not found.')