dofile(LockOn_Options.common_script_path.."elements_defs.lua")
dofile(LockOn_Options.script_path.."HUD/HUD_localizer.lua")

tex_scale = 0.35
pitch_K = 0.1062



TA_text_pos_y = 0.798
interline     = 0.686 - 0.798


use_mipfilter = true

modification_prop = get_aircraft_property_or_nil("modification") or "Ka-50_3"

HUD_FOV_LEVEL = 2
HUD_DEFAULT_LEVEL = HUD_FOV_LEVEL + 1

drawDbgGeometry = false
drawDbgMarkers 	= false

function AddWithDefaultClip(obj)
	obj.h_clip_relation = h_clip_relations.COMPARE
    obj.level 			= HUD_DEFAULT_LEVEL
	Add(obj)
end


--[[
  SetScale(MILLYRADIANS)
	next elements will be scaled in millyrads
  SetScale(METERS)
        next elements will be scaled in meters
  SetScale(FOV)
        and again in FOV parts
--]]

SetScale(FOV) -- MILLIRADIANS

--recalibrated for new logic in collimator

bird_position 				  = {0,-pitch_K * math.rad(10)/GetScale(),0}
vertical_displacement 		  = bird_position[1] - 0.517


heading_tape_vert_pos	      = 0.97

-- pixels
texture_resolution 	  	 	  = 1024

local fontScale 			  = 0.007899
default_font_stringdefs 	  = {fontScale, fontScale, 0, 0}

-- A fix after font resize
txtVertShift_Fix		  	  = 0.01

diamond_x_size 				  = 0.035
diamond_y_size 				  = diamond_x_size

function getTexParams(x, y)
	return {x / texture_resolution, y / texture_resolution, tex_scale, tex_scale}
end

piper_center_x = 910
piper_center_y = 154
piper_size     = 24 / texture_resolution

piper_tick_vertices = {{-0.2*(piper_size/tex_scale ), 0.9 * (piper_size/tex_scale)},
						{-0.2*(piper_size/tex_scale ), 1.3 * (piper_size/tex_scale)},
						{ 0.2*(piper_size/tex_scale ), 1.3 * (piper_size/tex_scale)},
						{ 0.2*(piper_size/tex_scale ), 0.9 * (piper_size/tex_scale)}
					  }

longestLine_coord_x = 3.4
longestLine_coord_y = 842.38

deviation_idx_left_coord_x = 724
deviation_idx_left_coord_y = 566

deviation_idx_left_verts   = {{-0.017, -0.03}, {-0.017, 0.03}, {0.03, 0.03}, {0.03, -0.03}}

deviation_idx_right_coord_x = 750
deviation_idx_right_coord_y = deviation_idx_left_coord_y

deviation_idx_right_verts   = {{-0.03, -0.03}, {-0.03, 0.03}, {0.017, 0.03}, {0.017, -0.03}}

diamond_coord_x = 827
diamond_coord_y = 597.5

shkvalLimits_coord_x = 512

max_idx_coord_x = 723
max_idx_coord_y = 588

max_idx_verts = {{-0.009, -0.026}, {-0.009, 0.015}, {0.055, 0.015}, {0.055, -0.026}}

function addPlaceholder(name, init_pos, controllers)
	local placeholder       		= CreateElement "ceSimple"
	placeholder.name        		= name
	init_pos 						= init_pos or {0, 0}
	placeholder.init_pos    		= {init_pos[1], init_pos[2], 0}
	placeholder.collimated  		= true
	
	if controllers ~= nil then
		placeholder.controllers		= controllers
	end
	
	Add(placeholder)
	return placeholder
end

function add_piper(name, controller)
	local piper				= create_HUD_element("ceTexPoly")
	piper.name				= name
	piper.vertices			= {{-piper_size/tex_scale, -piper_size/tex_scale}, 
							   {-piper_size/tex_scale, 1.32*piper_size/tex_scale},
							   {piper_size/tex_scale,  1.32*piper_size/tex_scale},
							   {piper_size/tex_scale, -piper_size/tex_scale}
							  }
	piper.indices			= default_box_indices
	piper.material			= "INDICATION_HUD_KA50"
	piper.tex_params		= getTexParams(piper_center_x, piper_center_y)
	piper.controllers		= controller
	AddWithDefaultClip(piper)
	
	return piper
end					

function add_piper_distance_sector_basic(name, parent,controls)
local distance_sector			= create_HUD_element("ceHWSector")
	
	local maskSz 					= 0.081
	local maskGap 					= 0.007

	distance_sector.name			= name
	distance_sector.vertices1		= {{-maskGap, -maskSz}, {-maskGap, maskSz}, {maskSz, maskSz}, {maskSz, -maskSz}}
	distance_sector.tex_params1		= getTexParams(986.5, piper_center_y)
	distance_sector.vertices2		= {{-maskSz, -maskSz}, {-maskSz, maskSz}, {maskGap, maskSz}, {maskGap, -maskSz}}
	distance_sector.tex_params2		= getTexParams(976.11, piper_center_y)
	distance_sector.maskvertices	= {{-maskSz, -maskSz}, {-maskSz, maskSz}, {0.0, maskSz}, {0.0, -maskSz}}
	distance_sector.indices			= default_box_indices
	distance_sector.material		= "INDICATION_HUD_KA50"
	distance_sector.controllers     = controls
	distance_sector.parent_element	= parent
	distance_sector.minangle		= math.rad(2.0)
	distance_sector.maxangle		= math.rad(358.0)
	AddWithDefaultClip(distance_sector)
	
	return distance_sector
end

local pipperRngTickXcoord   = 941

function add_piper_range_tick_basic(name, parent, init_angle,controls)
local tick				= create_HUD_element("ceTexPoly")
	tick.name				= name
	tick.vertices			= piper_tick_vertices
	tick.indices			= default_box_indices
	tick.material		    = "INDICATION_HUD_KA50"
	tick.tex_params			= getTexParams(pipperRngTickXcoord, piper_center_y)
	tick.init_rot			= {init_angle, 0.0, 0.0}
	tick.parent_element		= parent
	tick.controllers		= controls
	AddWithDefaultClip(tick)
	
	return tick
end

function add_piper_DLZ_tick_basic(name, parent,controls)
local tick				    = create_HUD_element("ceTexPoly")
	tick.name				= name
	tick.vertices			= piper_tick_vertices
	tick.indices			= default_box_indices
	tick.material		    = "INDICATION_HUD_KA50"
	tick.tex_params			= getTexParams(pipperRngTickXcoord, piper_center_y)
	tick.parent_element		= parent
	tick.controllers		= controls
	AddWithDefaultClip(tick)
	
	return tick
end

function add_piper_DLZ_tick(name, parent, tick_number ,max_distance)
	return add_piper_DLZ_tick_basic(name, parent,{{"show_piper_launch_zone"},
												  {"piper_dynamic_launch_zone", max_distance, (math.pi * 2.0) / max_distance , tick_number}})
end

function add_piper_distance_sector(name, parent)
	return add_piper_distance_sector_basic(name, parent,{{"show_piper_launch_zone"}, {"piper_distance_sector", 10000.0, (math.pi * 2.0) / 10000.0}})
end

function add_piper_range_tick(name, parent, init_angle)
	return add_piper_range_tick_basic(name, parent, init_angle,{{"show_piper_launch_zone"}})
end

function create_HUD_element(element_type)
	local element			= CreateElement(element_type)
	element.additive_alpha	= true
	element.collimated		= true
	element.use_mipfilter	= use_mipfilter
	
	return element
end

function add_debug_marker(name, init_pos, span_x, span_y, label_txt, label_shift)
	if drawDbgMarkers then
		
		local material = MakeMaterial("<REF_LINES>", {200, 50, 200, 255})
		local font 	   = MakeFont({used_DXUnicodeFontData = "font_dejavu_lgc_sans_22_white"}, {200, 50, 200, 255})

		-- cross at the marker position
		
		local cross           = CreateElement "ceMeshPoly"
		cross.name   		  = "dbg_ctr_".. (name or label_txt)
		cross.primitivetype   = "lines"
		cross.vertices        = {{-0.5 * span_x, 0.0}, {0.5 * span_x, 0.0}, {0, -0.5 * span_y}, {0, 0.5 * span_y}}
		cross.indices         = {0, 1, 2, 3}
		cross.material        = material
		cross.init_pos        = init_pos or {0, 0}
		cross.collimated      = true
		Add(cross)
		
		-- marker label text
		
		if label_txt ~= nil then
			local label			  = CreateElement "ceStringPoly"
			label.name   		  = "dbg_lbl_".. (name or label_txt)
			label.alignment		  = "LeftCenter"
			label.value			  = label_txt or name or "<REF>"
			label.material		  = font
			label.stringdefs	  = {0.01, 0.003, 0, 0}
			label.use_mipfilter   = true
			
			if label_shift ~= nil then
				label.init_pos        = label_shift
			else
				label.init_pos        = {0.5 * span_x, 0}
			end
			
			label.parent_element  = cross.name
			label.collimated	  = true
			Add(label)
		end
		
		return cross
	end
	
	return nil
end

function addDebugGeometryFor(element)
	if drawDbgGeometry then
		local dbg_box 			= CreateElement "ceMeshPoly"
		dbg_box.name 			= element.name .. "_dbg_box"
		dbg_box.primitivetype 	= "triangles"
		dbg_box.vertices 		= element.vertices
		dbg_box.indices			= default_box_indices
		dbg_box.material		= "RED_DBG"
		dbg_box.isdraw			= true
		dbg_box.change_opacity	= false
		dbg_box.collimated		= true
		dbg_box.h_clip_relation = h_clip_relations.NONE
	
		dbg_box.parent_element  = element.name
	
		Add(dbg_box)
		
		if drawDbgMarkers then
			local dbg_marker = add_debug_marker(element.name.."_dbg_marker_ctr", {0, 0}, 0.2, 0.2)
			dbg_marker.parent_element  = dbg_box.name
			dbg_marker.h_clip_relation = h_clip_relations.NONE
		end
	end
end

function addDebugGeometryForced(element)
	drawDbgGeometry 			= true
	drawDbgMarkers 				= true
	
	addDebugGeometryFor(element)
	
	drawDbgMarkers 				= false
	drawDbgGeometry 			= false
end

default_eye_distance = 0.58542335
hud_zone = default_eye_distance*math.tan(math.rad(11.2))
