//init.sqf

// m2 animate ["MainTurret", 800];
// m2 animate ["upper_motion", -800];


#include "\x\ace\addons\sys_arty_aimingpoints\script_component.hpp"
m2 addEventHandler ["GetIn", { _this call FUNC(aimingCircleGetIn); }];
m2 addEventHandler ["GetOut", { _this call FUNC(aimingCircleGetOut); }];

GVAR(aimingCircleViewIndex) = 1;
GVAR(aimingCircleCurrentViews) = nil;
GVAR(aimingCircleCurrentDeflection) = 0;
GVAR(aimingCircleCurrentDeflectionFine) = 0;

GVAR(aimingCircleCurrentElevation) = 0;
GVAR(aimingCircleCurrentElevationFine) = 0;

GVAR(aimingCircleCurrentOrientation) = 0;


GVAR(aimingCircleMainViews) = [
									[["view_elevation", "gunEnd"], 0.5, 0, [], 0, [], 0],
									["gunnerview", 0.5, 0, [], 0, [], 0],
									[["view_deflection", "centerpoint"], 0.45, 0, [], 0, [], 0],
									[["view_needle", "view_target_reticle"], 0.44, 0, [], 0, [], 0]
							];
							
//
GVAR(currentAimingCircle) = nil;
GVAR(aimingCircleCam) = nil;
GVAR(aimingCircleDisplayOpen) = false;
GVAR(aimingCircleActionIds) = [];
FUNC(aimingCircleGetOut) = {
	PARAMS_3(_circle,_pos,_unit);

	if (_unit == player) then {
		{
			GVAR(currentAimingCircle) removeAction _x;
		} forEach GVAR(aimingCircleActionIds);
		GVAR(aimingCircleActionIds) = [];
		GVAR(currentAimingCircle) = nil;
	};
};

FUNC(aimingCircleCloseDisplay) = {
	GVAR(aimingCircleCam) cameraEffect ["terminate", "back"];
	cutrsc ["default", "PLAIN DOWN"];
	camDestroy GVAR(aimingCircleCam);
	GVAR(aimingCircleCam) = nil;
	GVAR(currentAimingCircle) switchCamera "INTERNAL";
	// player action ["GetOut", GVAR(currentAimingCircle)];
	GVAR(aimingCircleDisplayOpen) = false;
};
	
FUNC(aimingCircleInitDisplay) = {
	GVAR(aimingCircleDisplayOpen) = true;
	[GVAR(aimingCircleMainViews), GVAR(aimingCircleViewIndex)] call FUNC(aimingCircleSetView);
};

FUNC(aimingCircleSetView) = {
	_views = _this select 0;
	GVAR(aimingCircleCurrentViews) = _views;
	_index = _this select 1;
	_selectedView = _views select _index;
	if(IS_ARRAY((_selectedView select 0))) then {
		GVAR(currentAimingCircle) switchCamera "INTERNAL";
		_viewPositions = _selectedView select 0;
		_snap = false;
		_camPos = (GVAR(currentAimingCircle) modelToWorld (GVAR(currentAimingCircle) selectionPosition (_viewPositions select 0)));
		_targetPos = (GVAR(currentAimingCircle) modelToWorld (GVAR(currentAimingCircle) selectionPosition (_viewPositions select 1)));
		diag_log text format["_camPos: %1, _targetPos: %2", _camPos, _targetPos];
		if(isNil QUOTE(GVAR(aimingCircleCam))) then {
			GVAR(aimingCircleCam) = "camera" camCreate _camPos;
			_snap = true;
			showCinemaBorder false;
		};
		preloadCamera _targetPos;
		5 preloadObject GVAR(currentAimingCircle);
		GVAR(aimingCircleCam) camPreload 5;
		GVAR(aimingCircleCam) camSetPos _camPos;
		GVAR(aimingCircleCam) cameraEffect ["internal", "back"];
		GVAR(aimingCircleCam) camSetTarget _targetPos;
		GVAR(aimingCircleCam) camSetFov (_selectedView select 1);
		if(_snap) then {
			GVAR(aimingCircleCam) camCommit 0;
		} else {
			GVAR(aimingCircleCam) camCommit (_selectedView select 2);
		};
	} else {
		GVAR(aimingCircleCam) cameraEffect ["terminate", "back"];
		cutrsc ["default", "PLAIN DOWN"];
		camDestroy GVAR(aimingCircleCam);
		GVAR(aimingCircleCam) = nil;
		if((_selectedView select 0) == "gunnerview") then {
			GVAR(currentAimingCircle) switchCamera "GUNNER";
		};
	};
};

FUNC(aimingCircleHandleAdjustKey) = {
	player sideChat format ["f: %1", _this];
	_key = _this select 1;
	_shift = _this select 2;
	_ctrl = _this select 3;
	_orient = false;
	if(GVAR(currentAimingCircle) animationPhase "right_cover" == 1) then {
		_orient = true;
	};
	
	if(_key == 205 || _key == 203) then {
		if(!_shift) then {
			_amount = 1;
			if(_ctrl) then {
				_amount = 0.25;
			};
			if(_key == 203) then {
				_amount = _amount*-1;
			};
			if(_orient) then {
				GVAR(aimingCircleCurrentOrientation) = GVAR(aimingCircleCurrentOrientation) + _amount;
				GVAR(currentAimingCircle) animate ["MainTurret", GVAR(aimingCircleCurrentOrientation)];
				GVAR(currentAimingCircle) animate ["orienting_knobs", GVAR(aimingCircleCurrentOrientation)];
			} else {
				GVAR(aimingCircleCurrentDeflectionFine) = GVAR(aimingCircleCurrentDeflectionFine) + _amount;
				GVAR(aimingCircleCurrentDeflection) = GVAR(aimingCircleCurrentDeflection) + _amount;
				GVAR(currentAimingCircle) animate ["upper_motion", GVAR(aimingCircleCurrentDeflection)];
				GVAR(currentAimingCircle) animate ["deflection_knob", GVAR(aimingCircleCurrentDeflectionFine)];
			};
		} else {
			_amount = 100;
			if(_key == 203) then {
				_amount = _amount*-1;
			};
			if(_orient) then {
				_currentPhase = GVAR(currentAimingCircle) animationPhase "MainTurret";
				player sideChat format["f: %1 c: %2", GVAR(aimingCircleCurrentOrientation), _currentPhase];
				if(_currentPhase == GVAR(aimingCircleCurrentOrientation)) then {
					GVAR(aimingCircleCurrentOrientation) = GVAR(aimingCircleCurrentOrientation) + _amount;
					GVAR(currentAimingCircle) animate ["MainTurret", GVAR(aimingCircleCurrentOrientation)];
					GVAR(currentAimingCircle) animate ["orienting_knobs", GVAR(aimingCircleCurrentOrientation)];
				};
			} else {
				_currentPhase = GVAR(currentAimingCircle) animationPhase "upper_motion";
				if(_currentPhase == GVAR(aimingCircleCurrentDeflection)) then {
					GVAR(aimingCircleCurrentDeflection) = GVAR(aimingCircleCurrentDeflection) + _amount;
					GVAR(currentAimingCircle) animate ["upper_motion", GVAR(aimingCircleCurrentDeflection)];
					GVAR(currentAimingCircle) animate ["deflection_knob_coarse", (GVAR(currentAimingCircle) animationPhase "deflection_knob_coarse")+(_amount/100)];
				};
			};
		};
	};
};

FUNC(aimingCircleHandleKey) = {
	_key = _this select 1;
	_move = false;
	if(_key in [203, 205, 200, 208]) then {
		_this call FUNC(aimingCircleHandleAdjustKey);
	} else {
		if(_key in (actionKeys "MoveForward")) then {
			GVAR(aimingCircleViewIndex) = (GVAR(aimingCircleViewIndex) - 1) max 0;
			_move = true;
		};
		if(_key in (actionKeys "MoveBack")) then {
			GVAR(aimingCircleViewIndex) = (GVAR(aimingCircleViewIndex) + 1) min ((count GVAR(aimingCircleCurrentViews))-1);
			_move = true;
		};
		if(_move) then {
			[GVAR(aimingCircleMainViews), GVAR(aimingCircleViewIndex)] call FUNC(aimingCircleSetView);
		};
	};
};

FUNC(aimingCircleMonitor) = {
	if(player in (crew GVAR(currentAimingCircle))) then {
		if(cameraView == "GUNNER") then {
			if(!GVAR(aimingCircleDisplayOpen)) then {
				createDialog "ace_sys_aimingpoints_aimingCircleDisplay";
			};
		};
		if(GVAR(aimingCircleDisplayOpen)) then {
			[GVAR(aimingCircleMainViews), GVAR(aimingCircleViewIndex)] call FUNC(aimingCircleSetView);
		};
	} else {
		[(_this select 1)] call CBA_fnc_removePerFrameHandler;
	};
};

FUNC(aimingCircleGetIn) = {
	PARAMS_3(_circle,_pos,_unit);

	if (_unit == player) then {
		GVAR(currentAimingCircle) = _circle;
		[FUNC(aimingCircleMonitor), 0] call CBA_fnc_addPerFrameHandler;
		_actionId = GVAR(currentAimingCircle) addAction ["Uncover Orienting Knobs", "fnc_uncoverKnobs.sqf", [], -100, false, true, "", "gunner _target == player && (_target animationPhase 'right_cover') == 0"];
		GVAR(aimingCircleActionIds) set[(count GVAR(aimingCircleActionIds)), _actionId];
		_actionId = GVAR(currentAimingCircle) addAction ["Cover Orienting Knobs", "fnc_coverKnobs.sqf", [], -100, false, true, "", "gunner _target == player && (_target animationPhase 'right_cover') == 1"];
		GVAR(aimingCircleActionIds) set[(count GVAR(aimingCircleActionIds)), _actionId];
		player sideChat format["tt: %1", GVAR(aimingCircleActionIds)];
		// createDialog "ace_sys_aimingpoints_aimingCircleDisplay";
	};
};