<?php

class UDMLController extends BaseController {

	public function doClass($funcType, $deviceType = '', $brand = '', $searchStr = '', $targetType = '', $dbType = '') {

		/*if(!Auth::check()) {
			return Response::json(array('result' => false, 'error' => 'not login'));
		}
		*/

		//$deviceType = urldecode($deviceType);
		//$brand = urldecode($brand);
		//$searchStr = urldecode($searchStr);
		if(!$this->chkJWTToken($errMsg, $errNo)) {
			if(!URCJWTWork::chkJWTTokenDealer($dealer_id, $dealer_grp_id, $dealer_grp_name, $errMsg, $errNo)) {
				return Response::make($errMsg, $errNo);
			}
			$this->username = $dealer_id;
			$this->usr_id = $dealer_id;
			$this->usr_level = URCValues::LOGIN_INSTALLER;
		}
		
		if($funcType == 'device_types') {

			$udml = new URCUDMLData($this->username/*//JWT->Auth::id()*/, 'dev');

			$result = $udml->getDeviceTypes();

			if($result == false) {
				return Response::json(array('result' => false, 'error' => 'fail to get device types'));
			}

			$return = array();
			$return['result'] = true;
			$return['types'] = $result;
			return Response::json($return);
		}

		if($funcType == 'brands') {
			
			if(strlen($deviceType) < 1) {
				return false;
			}


			$udml = new URCUDMLData($this->username/*//JWT->Auth::id()*/, 'dev');

			$result = $udml->getBrands($deviceType);
			if($result == false) {
				return Response::json(array('result' => false, 'error' => 'fail to get brands'));
			}

			$return = array();
			$return['result'] = true;
			$return['brands'] = $result;
			return Response::json($return);
		}

		if($funcType == 'devices') {

			if(strlen($deviceType) < 1 || strlen($brand) < 1) {
				return false;
			}

			$udml = new URCUDMLData($this->username/*//JWT->Auth::id()*/, 'dev');

			$result = $udml->getDevices($deviceType, $brand);
			if($result == false) {
				return Response::json(array('result' => false, 'error' => 'fail to get devices'));
			}

			$return = array();
			$return['result'] = true;
			$return['devices'] = $result;
			return Response::json($return);
		}

		if($funcType == 'search') {

			if(strlen($deviceType) < 1 || strlen($brand) < 1) {
				return false;
			}


			$udml = new URCUDMLData($this->username/*//JWT->Auth::id()*/, 'dev');

			$result = $udml->searchDevices($deviceType, $brand, $searchStr);
			if($result == false) {
				return Response::json(array('result' => false, 'error' => 'fail to search devices'));
			}

			$return = array();
			$return['result'] = true;
			$return['devices'] = $result;
			return Response::json($return);
		}

		if($funcType == 'search_all') {

			if(strlen($deviceType) < 1 || strlen($brand) < 1) {
				return false;
			}


			$udml = new URCUDMLData($this->username/*//JWT->Auth::id()*/, 'dev');

			$result = $udml->searchDevicesAll($deviceType, $brand);
			if($result == false) {
				return Response::json(array('result' => false, 'error' => 'fail to search devices'));
			}

			$return = array();
			$return['result'] = true;
			$return['devices'] = $result;
			return Response::json($return);
		}

		if($funcType == 'search_popular') {

			if(strlen($deviceType) < 1 || strlen($brand) < 1) {
				return false;
			}


			$udml = new URCUDMLData($this->username/*//JWT->Auth::id()*/, 'dev');

			$result = $udml->searchDevicesPopular($deviceType, $brand);
			if($result == false) {
				return Response::json(array('result' => false, 'error' => 'fail to search devices'));
			}

			$return = array();
			$return['result'] = true;
			$return['devices'] = $result;
			return Response::json($return);
		}

		if($funcType == 'device') {

			if(strlen($deviceType) < 1 || strlen($brand) < 1 || strlen($searchStr) < 1 || strlen($targetType) < 1 ||
				strlen($dbType) < 0) {
				return false;
			}

			$udml = new URCUDMLData($this->username/*//JWT->Auth::id()*/, 'dev');

			$result = $udml->getDevice($dbType, $searchStr, $targetType);
			if($result == false) {
				return Response::json(array('result' => false, 'error' => 'fail to get devices'));
			}

			$return = array();
			$return['result'] = true;
			$return['device'] = $result;

			//ErrorDebug::write(json_encode($return));
			return Response::json($return);
		}
	}

}