<?php
class Tag{
	function get($no){
		$r = query('select * from tag where no='.$no);
		return $r[0];
	}
	function add($a){
		$isnew=query('select * from tag where name=:name', $a);
		if(!$isnew[0]){
			$r = query("insert into tag set name=:name, memo=:memo", $a);
			reply_json($this->get($r[lastInsertId]));
		}
	}
	function getTagbyparam($a){
		/*
		if(isset($a[user_no]))
			$condition = $condition.' and user_no='.$a[user_no];
		if(isset($a[username])){
			$r = query('select no from info where username=:username', $a);
			$a[user_no] = $r[0][no];
		}	
		*/
		if(isset($a[playlistno]))
			$condition = $condition.' and playlist_no ='.$a[playlistno];
		if(isset($a[trackno])){
			$r = query('select playlistno from track where no=:trackno', $a);
			$a[playlistno] = $r[0][playlistno];
			$condition = $condition.' and playlist_no ='.$a[playlistno];	
		}
		$r = query("select DISTINCT tag from playlist_tag where 1 ".$condition);
		$tags = array();
		if(!empty($r)){	
			foreach($r as $tag){
				array_push($tags, $tag[tag]);
			}
		}
		return $tags;	
	}	
	function getTagbyPlaylist($playlist_no){
		$r = query('select tag from playlist_tag where playlist_no='.$playlist_no);
		$tags = array();
		if(!empty($r)){	
			foreach($r as $tag){
				array_push($tags, $tag[tag]);
			}
		}
		return $tags;	
	}		
	function addTagToPlaylist($a){
		if(isset($a[tags])){
			if(!empty($a[tags])){
				$tags=explode(',', $a[tags]);
				$a[playlist_no]=$a[no];
				$a[memo] = '';
				foreach($tags as $t){
					$a[tag] = $t;
					$a[name] = $t;
					query('insert into tag set name=:name, memo=:memo on duplicate key update name=:name', $a);
					query('insert into playlist_tag set playlist_no=:playlist_no, tag=:tag on duplicate key update tag=:tag', $a);		
				}
			}
		}
		
	}	
	function deleteTagInPlaylist($a){
		$tags=explode(',', $a[tags]);
		$tag = new Tag();
		$origintags=$tag->getTagbyPlaylist($a[no]);
		$remove_tags = array_diff($origintags, $tags);
		foreach($remove_tags as $t){
			$a[tag] = $t;
			$r = query("delete from playlist_tag where tag=:tag and playlist_no=:no", $a);
		}
	}	
	function search($a){
		//$a[name] = "$a[name]%";
		$resultList=query('select playlist_no as no, title,user_no,memo,coverimg,(select count(*) from track t where P.no = t.playlistno) as totalcount from playlist P join (select playlist_no from playlist_tag where tag like :name) T where T.playlist_no = P.no', $a);
		if(!$resultList[0]){
			reply_json(response(404));
			return;
		}
		else
		{
			for($i=0;$i<count($resultList);$i++)
			{
				$user_name = query('select username from info where no = '.$resultList[$i][user_no]);
				$resultList[$i][user_no] = $resultList[$i][user_no];
				$resultList[$i][username] = $user_name[0][username];
				if(preg_match('#^http#', $resultList[$i][coverimg]) != 1 && !empty($resultList[$i][coverimg])){
					$resultList[$i][coverimg] = 'http://roseimg.modtv.co.kr/playlist_cover/'.$resultList[$i][coverimg];
				}else{
					$track=query("select snippet from track where playlistno=".$resultList[$i][no]." limit 1 offset 0");
					$thumbnails = json_decode($track[0][snippet], true)[thumbnails];
					if(!empty($thumbnails['default'][url]))
						$resultList[$i][coverimg] = $thumbnails['default'][url];		
					if(!empty($thumbnails[medium][url]))
						$resultList[$i][coverimg] = $thumbnails[medium][url];
					if(!empty($thumbnails[high][url]))
						$resultList[$i][coverimg] = $thumbnails[high][url];					
				}
				$resultList[$i][tags] = $this->getTagbyPlaylist($resultList[$i][no]);
			}
			echo json_encode($resultList,JSON_UNESCAPED_UNICODE);
		}
	}
}

/*
	tag 전체 가져오기
*/
$app->action('tag/fetch', function() use($auth){
	$a = $_REQUEST;
	$tag = new Tag();
	$authorized = $auth->verify($a[accesskey]);	
	if($authorized['status'] == 'OK'){	
		$r=query("select * from tag", $a);
		reply_json($r);
	}else{
		reply_json($authorized);
	}
});

/* tag 추가 하기 */
$app->action('tag/add', function() use($auth){
	$authorized = $auth->verify($a[accesskey]);	
	$tag = new Tag();
	if($authorized['status'] == 'OK'){		
		$tag->add($_REQUEST);
	}else{
		reply_json($authorized);
	}
});

/* tag 검색 하기 */
$app->action('playlist/tag/search', function() use($auth){
	$a = $_REQUEST;
	$tag = new Tag();
	$authorized = $auth->verify($a[accesskey]);	
	if($authorized['status'] == 'OK'){	
		$tag->search($_REQUEST);
	}else{
		reply_json($authorized);
	}
});

?>