//======================================================================================================================
//
// Selects the resource for extraction
//
void	ObjectController::_handleHarvesterSelectResource(uint64 targetId,Message* message,ObjectControllerCmdProperties* cmdProperties)
{
    CreatureObject* creature  = dynamic_cast<CreatureObject*>(mObject); PlayerObject* player = creature->GetGhost();

    if(!player)
    {
        return;
    }

    //do we have a valid structure ???
    uint64 id = targetId;
    Object* object = gWorldManager->getObjectById(id);
    PlayerStructure* structure = dynamic_cast<PlayerStructure*>(object);

    if(!structure)
    {
        //gMessageLib->sendSystemMessage(player,L"","player_structure","command_no_building");
        return;
    }

    //is the structure in Range???
    float fTransferDistance = gWorldConfig->getConfiguration<float>("Player_Structure_Operate_Distance",(float)10.0);
    if(glm::distance(player->GetCreature()->mPosition, structure->mPosition) > fTransferDistance)
    {
        DLOG(info) << " ObjectController::_handleHarvesterSelectResource Structure not in Range";
        return;
    }

    HarvesterObject* harvester = dynamic_cast<HarvesterObject*>(structure);

    //get the relevant Resource
    BString dataStr;
    message->getStringUnicode16(dataStr);

    uint64 resourceId;
    swscanf(dataStr.getUnicode16(),L"%"WidePRIu64,&resourceId);

    Resource* tmpResource = gResourceManager->getResourceById(resourceId);

    if((!tmpResource)||(!tmpResource->getCurrent()))
    {
        DLOG(info) << " ObjectController::_handleHarvesterSelectResource No valid resource!";
        return;
    }

    harvester->setCurrentResource(resourceId);

    // update the current resource in the db
    mDatabase->executeSqlAsync(0,0,"UPDATE %s.harvesters SET ResourceID=%"PRIu64" WHERE id=%"PRIu64" ",mDatabase->galaxy(),resourceId,harvester->getId());

    CurrentResource* cR = reinterpret_cast<CurrentResource*>(tmpResource);
    //resource = reinterpret_cast<CurrentResource*>(gResourceManager->getResourceByNameCRC(resourceName.getCrc()));

    float posX, posZ;
    float ratio = 0.0;

    posX	= harvester->mPosition.x;
    posZ	= harvester->mPosition.z;


    if(cR)
    {
        ratio	= cR->getDistribution((int)posX + 8192,(int)posZ + 8192);
        if(ratio > 1.0)
        {
            ratio = 1.0;
        }
    }

    float ber = harvester->getSpecExtraction();

    harvester->setCurrentExtractionRate(ber*ratio);

    // now enter the new resource in the hoppers resource list if its isnt already in there
    // TODO keep the list up to date by removing unnecessary resources
    // to this end read the list anew and delete every resource with zero amount
    // have a stored function do this

    if(!harvester->checkResourceList(resourceId))
    {
        //do *not* add to list - otherwise we get a racecondition with the asynch update from db !!!
        //harvester->getResourceList()->push_back(std::make_pair(resourceId,float(0.0)));
        //add to db
        mDatabase->executeSqlAsync(0,0,"INSERT INTO %s.harvester_resources VALUES(%"PRIu64",%"PRIu64",0,0)",mDatabase->galaxy(),harvester->getId(),resourceId);
     
    }

    // update the current extractionrate in the db for the stored procedure handling the harvesting
    mDatabase->executeSqlAsync(0,0,"UPDATE %s.harvesters SET rate=%f WHERE id=%"PRIu64" ",mDatabase->galaxy(),(ber*ratio),harvester->getId());
    

    //now send the updates
    gMessageLib->sendCurrentResourceUpdate(harvester,player);
    gMessageLib->sendCurrentExtractionRate(harvester,player);

}