<?php

namespace AppBundle\Controller;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Happyr\LinkedIn\LinkedIn;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
    Symfony\Component\HttpFoundation\Request,
    Symfony\Component\HttpFoundation\Response,
    DateTime,
	AppBundle\Entity\Event,
	AppBundle\Entity\Applicant,
	AppBundle\Entity\Job;

class JobController extends Controller
{
	public function updateJobAction(Request $request)
    {
	$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
	$path = $baseurl . '/bundles/app/js/jobs.json';
	//$content = file_get_contents($path); // allow_url_fopen =0   
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $path);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);

	$jobs = json_decode($content, true);
	$i = 0;
	foreach ($jobs as $job) {
		foreach ($job as $itemjob) {
		$ev = new Job();
		echo $itemjob['company']['name']; 
			$ev->setName($itemjob['position']['title'])
				->setCompany($itemjob['company']['name'])
				->setPublished(true)
				//->setAuthoremail($itemjob['jobPoster']["firstName"] . " ". $itemjob['jobPoster']["lastName"])
				->setDeadline(new \DateTime($itemjob['expirationDate']['year'].'/'.$itemjob['expirationDate']['month'].'/'. $itemjob['expirationDate']['day']))
				->setJobDescription(strip_tags($itemjob['description'],'<p><a><ul><li><br><strong>'))
				->setContact($itemjob['jobPoster']["firstName"] . " ". $itemjob['jobPoster']["lastName"])
				->setLinkedinURL($itemjob['siteJobUrl']); 
				
					
			$repo = $this->getDoctrine()->getManager()->getRepository('AppBundle:Compagny');
            $query = $repo->createQueryBuilder('a')->where('a.name LIKE :name')->setParameter('name', '%'.$itemjob['company']['name'].'%')->getQuery();
			$company = $query->setMaxResults(1)->getOneOrNullResult();
			
			if($company) {
				$ev->setLatitude($company->getLatitude())
				->setLongitude($company->getLongitude())
				->setCity($company->getCity())
				->setZip($company->getZip())
				->setNumber($company->getNumber())
				->setLocationAdresse($company->getLocationAdresse())
				->setCompanyDescription($company->getActivity())
				->setStreet($company->getStreet());
			}
			
			$em = $this->getDoctrine()->getManager();
			$repository = $em->getRepository('AppBundle:Job');
			$em->persist($ev); 
			$em->flush();
		}
}
	
     /* $linkedIn = new LinkedIn('78664p6kacenh9', '7POfWVcWw3jHq8yJ');
	 // $linkedIn->setHttpClient(new \Http\Adapter\Guzzle6\Client());
	//$linkedIn->setHttpMessageFactory(new Http\Message\MessageFactory\GuzzleMessageFactory());
	
	if ($linkedIn->isAuthenticated()) {
		//we know that the user is authenticated now. Start query the API
		 $user = $linkedIn->get('v1/job-search:(jobs:(id,customer-job-code,active,posting-date,expiration-date,posting-timestamp,expiration-timestamp,company:(id,name),position:(title,location,job-functions,industries,job-type,experience-level),skills-and-experience,description-snippet,description,salary,job-poster:(id,first-name,last-name,headline),referral-bonus,site-job-url,location-description))?&company-name=Merck&country-code=ch');
		 dump($user);

	} else {
		 echo "User canceled the login.";
		 $url = $linkedIn->getLoginUrl();
	echo "<a href='$url'>Login with LinkedIn</a>";
	}*/
	   return new Response();
    }
}