<?php

require_once('URCSystemData.php');
require_once('DataGenerator.php');

class DealerDeviceController extends BaseController {
	
	public function doDeviceControl($funcType, $customDevId = -1) {

		$dealer_id = '';
		$dealer_grp_id = '';
		$dealer_grp_name = '';
		if(!URCJWTWork::chkJWTTokenDealer($dealer_id, $dealer_grp_id, $dealer_grp_name, $errMsg, $errNo)) {
			return Response::make($errMsg, $errNo);
		}

		if($funcType == 'list') {

			$urcCDevice = new URCCustomDevData($dealer_grp_id, $dealer_id);
			$result = $urcCDevice->getDeviceList();
			if($result === false) {
				return Response::json(array('result' => false, 'error' => 'fail to get the list'));
			}

			return Response::json(array('result' => true, 'devices' => $result));
		}

		if($funcType == 'create') {

			$rules = array(
				'name' => 'Required',
				'roomId' => 'Required|Integer',
				'deviceId' => 'Required|Integer');

			$v = Validator::make(Input::all(), $rules);
			if($v->fails()) {
				return Response::json(array('result' => false, 'error' => implode("\n", $v->messages()->all())));
			}

			$name = Input::get('name');
			$roomId = Input::get('roomId');
			$deviceId = Input::get('deviceId');

			$urcCDevice = new URCCustomDevData($dealer_grp_id, $dealer_id);
			$result = $urcCDevice->save($roomId, $deviceId, $name, $errMsg);
			if($result === false) {
				return Response::json(array('result' => false, 'error' => $errMsg));
			}

			return Response::json(array('result' => true));
		}

		if($funcType == 'create_from_template') {

			$rules = array(
				'name' => 'Required',
				'roomId' => 'Required|Integer',
				'deviceId' => 'Required|Integer');

			$v = Validator::make(Input::all(), $rules);
			if($v->fails()) {
				return Response::json(array('result' => false, 'error' => implode("\n", $v->messages()->all())));
			}

			$name = Input::get('name');
			$roomId = Input::get('roomId');
			$deviceId = Input::get('deviceId');

			$urcCDevice = new URCCustomDevData($dealer_grp_id, $dealer_id);
			$result = $urcCDevice->saveFromTemplate($roomId, $deviceId, $name, $errMsg);
			if($result === false) {
				return Response::json(array('result' => false, 'error' => $errMsg));
			}

			return Response::json(array('result' => true));
		}

		if($funcType == 'delete') {

			if($customDevId < 0) {
				return Response::json(array('result' => false, 'error' => 'custom Device Id is wrong'));
			}

			$urcCDevice = new URCCustomDevData($dealer_grp_id, $dealer_id);
			$result = $urcCDevice->delete($customDevId);

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

			return Response::json(array('result' => true));
		}

		if($funcType == 'get_device_info') {

			if($customDevId < 0) {
				return Response::json(array('result' => false, 'error' => 'custom Device Id is wrong'));
			}
			
			$urcCDevice = new URCCustomDevData($dealer_grp_id, $dealer_id);
			$result = $urcCDevice->getDeviceInfo($customDevId);

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

			return Response::json(array('result' => true, 'data' => $result));
		}
		
		if($funcType == 'apply') {
			
			if($customDevId < 0) {
				return Response::json(array('result' => false, 'error' => 'custom Device Id is wrong'));
			}

			$rules = array(
				'name' => 'Required',
				'targetType' => 'Required',
				'targetAddr' => 'Required',
				'targetPort' => 'Required|Integer',
				'favEnable' => 'Required',
				'favDelay' => 'Required',
				'favEnter' => 'Required',
				'roomId' => 'Required',
				'repeatMin' => 'Required|Integer',
				'repeatMacro' => 'Required|Integer');

			$v = Validator::make(Input::all(), $rules);
			if($v->fails()) {
				return Response::json(array('result' => false, 'error' => implode("\n", $v->messages()->all())));
			}

			$name = Input::get('name');
			$target = new stdClass;
			$target->type = Input::get('targetType');
			$target->addr = Input::get('targetAddr');
			$target->port = Input::get('targetPort');

			$attr = new stdClass;
			$attr->repeat_min = Input::get('repeatMin');
			$attr->repeat_macro = Input::get('repeatMacro');			
			if($target->type == URCValues::DB_TYPE_IP ||
				$target->type == URCValues::DB_TYPE_UDP) {
				
				$rules2 = array(
					'rampStart' => 'Required|Integer',
					'rampSpeed' => 'Required|Integer',
					'autoCr' => 'Required');

				$v = Validator::make(Input::all(), $rules2);
				if($v->fails()) {
					return Response::json(array('result' => false, 'error' => implode("\n", $v->messages()->all())));
				}

				$attr->ramp_start = Input::get('rampStart');
				$attr->ramp_speed = Input::get('rampSpeed');
				$attr->auto_cr = Input::get('autoCr');	
			}

			$favOption =  new stdClass;
			$favOption->enable = Input::get('favEnable');
			if($favOption->enable) {
				$favOption->delay =  Input::get('favDelay');
			}
			else {
				$favOption->delay = 400;
			}
			$favOption->enter = Input::get('favEnter');

			$roomId = Input::get('roomId');

			/*
			$userInfo = Input::get('user_info');
			
			try {
				if(URCJWTWork::parseJWTToken($userInfo, $username, $id, $level, $errMsg, $errNo) === false) {
					return Response::json(array('result' => false, 'error' => $errMsg));
				}
		        
		    } catch (Exception $e) {
		    	if ($e->getMessage() === 'Expired Token') {
		    		$errMsg = 'Expired Token';
					return Response::json(array('result' => false, 'error' => $errMsg));
		    	} else {
		    		$errMsg = 'user not found';
					return Response::json(array('result' => false, 'error' => $errMsg));
		    	}
	    	}
	    	*/

			$urcCDevice = new URCCustomDevData($dealer_grp_id, $dealer_id);
			$result = $urcCDevice->apply($customDevId, $roomId, $name, $target, $attr, $favOption, $errMsg);

			if($result === false) {
				return Response::json(array('result' => false, 'error' => $errMsg ));
			}

			return Response::json(array('result' => true));
		}

		if($funcType == 'apply_to_template') {
			
			if($customDevId < 0) {
				return Response::json(array('result' => false, 'error' => 'custom Device Id is wrong'));
			}

			$rules = array(
				'name' => 'Required',
				'targetType' => 'Required',
				'targetAddr' => 'Required',
				'targetPort' => 'Required|Integer',
				'favEnable' => 'Required',
				'favDelay' => 'Required',
				'favEnter' => 'Required',
				'roomId' => 'Required',
				'repeatMin' => 'Required|Integer',
				'repeatMacro' => 'Required|Integer');

			$v = Validator::make(Input::all(), $rules);
			if($v->fails()) {
				return Response::json(array('result' => false, 'error' => implode("\n", $v->messages()->all())));
			}

			$name = Input::get('name');
			$target = new stdClass;
			$target->type = Input::get('targetType');
			$target->addr = Input::get('targetAddr');
			$target->port = Input::get('targetPort');

			$attr = new stdClass;
			$attr->repeat_min = Input::get('repeatMin');
			$attr->repeat_macro = Input::get('repeatMacro');			
			if($target->type == URCValues::DB_TYPE_IP ||
				$target->type == URCValues::DB_TYPE_UDP) {
				
				$rules2 = array(
					'rampStart' => 'Required|Integer',
					'rampSpeed' => 'Required|Integer',
					'autoCr' => 'Required');

				$v = Validator::make(Input::all(), $rules2);
				if($v->fails()) {
					return Response::json(array('result' => false, 'error' => implode("\n", $v->messages()->all())));
				}

				$attr->ramp_start = Input::get('rampStart');
				$attr->ramp_speed = Input::get('rampSpeed');
				$attr->auto_cr = Input::get('autoCr');	
			}

			$favOption =  new stdClass;
			$favOption->enable = Input::get('favEnable');
			if($favOption->enable) {
				$favOption->delay =  Input::get('favDelay');
			}
			else {
				$favOption->delay = 400;
			}
			$favOption->enter = Input::get('favEnter');

			$roomId = Input::get('roomId');

			$urcCDevice = new URCCustomDevData($dealer_grp_id, $dealer_id);
			$result = $urcCDevice->applyToTemplate($customDevId, $roomId, $name, $target, $attr, $favOption, $errMsg);

			if($result === false) {
				return Response::json(array('result' => false, 'error' => $errMsg ));
			}

			return Response::json(array('result' => true));
		}
	}
}