<?php


$qual_list = [
	'TS 720p'         => 0,
	'TS '             => 1,	
	'CAMRip'          => 2,
	'Workprint-AVC'   => 3,
	'WEB-DLRip 720p'  => 4,
	'WEB-DLRip 1080p' => 5,
	'WEB-DLRip 1080p' => 6,
	'WEB-DLRip'       => 7,
	'VHSRip'          => 8,
	'TVRip 720p'      => 9,
	'TVRip'           => 10,
	'SuperTS'         => 11,
	'SATRip'          => 12,
	'Laserdisc-RIP'   => 13,
	'IPTVRip'         => 14,
	'DVDSrc'          => 15,
	'DVDRip'          => 16,
	'DVBRip 720p'     => 17,
	'DVBRip'          => 18,
	'HDDVDRip 720p'   => 19,
	'HDDVDRip 1080p'  => 20,
	'HDDVDRip'        => 21,
	'HDTVRip 720p'    => 22,
	'HDTVRip 1080p'   => 23,
	'HDTVRip'         => 24,
	'HDRip 720p'      => 25,
	'HDRip 1080p'     => 26,
	'HDRip'           => 27,
	'D-VHS'           => 28,
	'BDRip 720p'      => 29,
	'BDRip 1080p'     => 30,
	'BDRip'           => 31
];


class Kodik {

	private $db;
	private $log;
	private $next;
	private $type;
	
	function __construct($type, $db, $config_kodik){
		$this->log = unserialize( file_get_contents( ENGINE_DIR . '/data/kodik.log' ) );
		if ( !$this->log ) $this->log = array();

        $this->next = !empty($this->log[$type]['next']) ? $this->log[$type]['next'] : '';

		$this->db = $db;
		$this->type = $type;
	}

	function start($url){
        echo $url;
	    for ($i=0; $i < 5; $i++) { 

	    	$result = request($url);
	    	$res = json_decode($result, true);

	    	if(!$res) die($result);
	    	if(empty($res['results'])) {
	    		$this->logs($this->type, 'next', '');
	    		die('empty results');
	    	}

	        $this->parse($res['results']);

	        $url = ($i==0 && $this->next) ? $this->next : $res['next_page'];
	  	    $this->logs($this->type, 'next', $res['next_page']);
	        if(!$url) break;
	    }

	}


    function parse($result){
	    foreach ($result as $k => $value) {
	    	$kodik_id     = $value['id'];
	        $kp_id        = intval($value['kinopoisk_id']);
	        $shikimori_id = intval($value['shikimori_id']);
	        $mdl_id       = intval($value['mdl_id']);
	        $year         = intval($value['year']);
	        $type         = $value['type'];
	        $quality      = addslashes($value['quality']);
	        $episode      = $season = 0;

	        global $qual_list;

	        if (!$kp_id AND !$shikimori_id AND !$mdl_id) {
	        	continue;
	        }

	        $row = $this->db->super_query( "SELECT * FROM " . PREFIX . "_kodik WHERE kp_id='$kp_id' AND shikimori_id='$shikimori_id' AND mdl_id='$mdl_id'" );

	        if ($row) {
	            if($qual_list[$row['quality']]<$qual_list[$quality] or $row['season']<$season or $row['episode']<$episode) {

            	    $this->db->query( "UPDATE " . PREFIX . "_kodik SET kodik_id='{$kodik_id}', status='1', quality='{$quality}', episode='{$episode}', season='{$season}' WHERE id='{$row['id']}'" );
	            }

	        } else {
	            $this->db->query( "INSERT INTO " . PREFIX . "_kodik (kp_id, shikimori_id, kodik_id, year, quality, type, episode, season, mdl_id) VALUES('{$kp_id}', '{$shikimori_id}', '{$kodik_id}', '{$year}', '{$quality}', '{$type}', '{$episode}', '{$season}',  '{$mdl_id}')" );
	        }

	    }

    }

    function logs($type, $name, $val){
    	$this->log[$type][$name] = $val;
    	file_put_contents( ENGINE_DIR . '/data/kodik.log', serialize($this->log));
    }

}


?>