function TargetSelection(_par, _notify, _data)
{
	var th = this
//--------------------------------//
	//////////////////////////////////////////
	var allTime = __config.time_for_second_step
	var active_ready = false
	var target_user
	var target_bot
	var multibomb_user
	var bot_targets = new Array()
	var user_targets = new Array()
	var per_active = false
	// var dinamite_user = new Array()
	// var periskope_user = new Array()
	// var multibomb_bot = new Array()
	// var dinamite_bot = new Array()
	// var periskope_bot = new Array()
	var selected_objects = new Array()
	var bot_selected_objects = new Array()
	var periskop_active = null
	var periskop2 = null
	var periskop_view_user = new Array()
	var periskop_view_bot = new Array()
	var periskope_user, periskope_bot;
	var dimension = _par.getDimension()
	////////////////////////////////////
	this.getData = function(){
		var p = _data
		// 2step
		p.target_user = target_user
		p.target_bot = target_bot
		// Выбранные объект
		p.selected_objects = selected_objects
		p.bot_selected_objects = bot_selected_objects
		//Перископ
		p.periskop_active = periskop_active					// Функция
		p.periskop2 = periskop2
		p.periskop_view_user = periskop_view_user			// Отображаемые объекты
		p.periskop_view_bot = periskop_view_bot
		// Цели
		p.bot_targets = bot_targets
		p.user_targets = user_targets

		return p
	}
// Создание красного крестика
	this.createTarget = function(player){
		if(per_active)
			return
		if(player == "user"){
			var i = Math.floor(dimension/2)-1
			var j = Math.floor(dimension/2)-1
			// var i = 0
			// var j = 0
			checkLightingIcon(true)
			target_user = new SeaObjects("anim-target", 1, "goriz")
			target_user.setTakes(i, j)
			set_ship_in_cell(i, j, target_user, player)
			selected_objects.push(target_user)
			_par.activeReady()
			active_ready = true
			_notify()
		}
	}

	this.createBonus = function(v, player){
		if(per_active)
			return
		// if(v=="torpedo")
		// 	return
		// console.log("create ", v)
		if(player == "user"){
			if(!active_ready && !target_user)
				_par.deActiveReady()
			var i = Math.floor(dimension/2)-1
			var j = Math.floor(dimension/2)-1
			checkLightingIcon()
			var bonus = new SeaObjects(v, 1, "goriz")
			createFilling(i, j, bonus)
			set_ship_in_cell(i, j, bonus, player)
			selected_objects.push(bonus)
			if(v == "periskop"){
				periskope_user = bonus
				_par.activeReady()
			}
			_notify()
		}
	}

	this.deActiveBonus = function(v){
		if(per_active)
			return
		checkLightingIcon()
		for(var a=0; a < selected_objects.length; a++)
			if(selected_objects[a].getName() == v)
				selected_objects(a, 1)
		_notify()
	}

// Изменение положения красного крестика на поле
	this.changeShipLoc = function(o, i, j, player){
		set_ship_in_cell(i, j, o, player)
	}
// Поворот корабля 
	this.rotateShip = function(o, player){
		var i = o.getTakes()[0].i
		var j = o.getTakes()[0].j
		if(o.getName() == "dinamite"){
			o.changeDir()
			set_ship_in_cell(i, j, o, player)
		}
		set_ship_in_cell(i, j, o, player)
	}
// Получение общего времени на шаг
	this.getAllTime = function(){
		return allTime
	}
	var aftertime = false
// Метод вызываемый по истечении времени
	this.after_time = function(){
		if(aftertime)
			return
		if(_data.num_attack<5)
			_par.setModal("text_target_placed", true)
		if(!target_user){
			th.createTarget("user")
			var randX = Math.round(Math.random()*(dimension - 1))
			var randY = Math.round(Math.random()*(dimension - 1))
			th.changeShipLoc(target_user, randX, randY, "user")
		}
		_par.activeReady()
		active_ready = true
		aftertime = true
		_par.nextStep()
	}
// 
	this.calculation = function(){
		var logic = new BotLogic(th, _data.made_moves_bot, _data.ships_user, _data.mines_user, _data.chests, _data.multibomb_num_bot, _data.dinamite_num_bot, _data.rocks, _data, _data.torpedo_num_bot)
		for(var a=0; a<selected_objects.length; a++){
			if(selected_objects[a].getName() != "periskop")
				user_targets.push(selected_objects[a].getTakes())			
		}
		for(var a = 0; a< user_targets.length; a++){
			if(user_targets[a].length == 3){
				_data.dinamite_num_user--
			}
			if(user_targets[a].length == 4)
				_data.multibomb_num_user--
			if(user_targets[a].length == 5)
				_data.torpedo_num_user--			
		}
		bot_targets = logic.getTargets()
		dinamite_num_bot = logic.getNumDinamit()
		multibomb_num_bot = logic.getNumBomb()
		torpedo_num_bot = logic.getNumTorpedo()
		// console.log("calc", _data.multibomb_num_bot, _data.dinamite_num_bot)
	}
//
	this.goPeriskope = function(){
		per_active = true
		_par.pause()
		periskop2 = true
		periskop_active = function(){
			var v = "user"
			var periskope = v == "user" ? periskope_user: periskope_bot
			var ships = v == "user" ? _data.ships_bot : _data.ships_user
			var mines = v == "user" ? _data.mines_bot : _data.mines_user
			var chest = v == "user" ? _data.chest_bot : _data.chest_user
			var periskop_view = v == "user" ? periskop_view_user : periskop_view_bot
			for(var c=0; c<periskope.getTakes().length; c++){
				var curI = periskope.getTakes()[c].i
				var curJ = periskope.getTakes()[c].j
				var ar = new Array()
				ar = ar.concat(ships)
				ar = ar.concat(mines)
				ar = ar.concat(chest)
				// ar = ar.concat(_data.chests)
				for(var a=0; a< ar.length; a++) {
					var m = ar[a].getTakes()
					for(var b = 0; b< m.length; b++)
						if(m[b].i == curI && m[b].j == curJ)
							createViewerObj(curI, curJ, ar[a].getName(), v)
				}
			}
			
			if(!active_ready)
				_par.deActiveReady()
			if(v == "user"){
				periskop2 = null
										for(var i = 0; i<selected_objects.length; i++){
											if(selected_objects[i].getName() == "periskop" && indexOf(_data.used_objects, selected_objects[i]) == -1){
												_data.periscopes.push(selected_objects[i])
												selected_objects.splice(i, 1)
											}
										}
				t_delayOn(__config.time_for_periscope, function(){
										periskop_active = null
										_data.periskope_num_user--
										periskop_view_user = new Array()
										checkLightingIcon()
										per_active = false
										_par.unPause()
										_notify()
									})
			}else
				periskope_num_bot--
			// console.log("finish_", selected_objects.length)
			_notify()
		}
		_notify()
	}

	this.getDimension = function(){
		return dimension
	}
// PRIVATE //
// Создание оюъектов при вызове перископа
	function createViewerObj(i, j, name, player){
		if(player == "user"){
			console.log(name)
			if(name == "ship")
				name = "part_ship"
			var bonus = new SeaObjects(name, 1, "goriz")
			bonus.setTakes(i, j)
			periskop_view_user.push(bonus)
			// bonus.getVisual().removeAllEventListeners()
			// dragAndDrop(bonus.getVisual())
		}
	}

// Установка объекта в ячейку
	function set_ship_in_cell(i, j, ship, player){
		var coor = searchSpace(i, j, ship, player)
		ship.setTakes(-1)
		// console.log(coor)
		createFilling(coor.i, coor.j, ship)
		_notify()
	}

// Проверка зажженности иконок
	function checkLightingIcon(target){
		for(var i = 0; i<selected_objects.length; i++){
			if(target){
				if(selected_objects[i].getName() == "anim-target"){
					selected_objects.splice(i, 1)
					i--
				}
			}else{
				if(selected_objects[i].getName() == "bomb" || selected_objects[i].getName() == "dinamite" || selected_objects[i].getName() == "periskop" || selected_objects[i].getName() == "torpedo"){
					selected_objects.splice(i, 1)
					i--
				}
			}
		}
	}

	// Поиск свободного места
	function searchSpace(i, j, ship, player, without_move) {
		if(!checkLoc(i, j, ship, player, without_move)){
			return {i:i, j: j}
		}

		for(var b = 1; b < dimension; b++){
			var search_mas = buildMasIndex(-1 * b, -1 * b, Math.abs((-1 * b)*2) + 1)
			for(var a=0; a<search_mas.length; a++){
				if(!checkLoc(i + search_mas[a][0], j + search_mas[a][1], ship, player, without_move))
					return {i:i + search_mas[a][0], j: j + search_mas[a][1]}
			}
		}
		if(!without_move)
			return searchSpace(i, j, ship, player, true)
		// return {i:i, j: j}
	}

	// Проверка ячейки для корабля с определенными габаритами
	function checkLoc(i, j, ship, player, without_move){
		var mas = ship.getTakes()
		var deaks = ship.getTakes().length
		var num = deaks
		// console.log("zdes", deaks)
		for(var a = 0; a<deaks; a++){
			// for()
			if(!checkCell(i + (mas[a].i - mas[0].i), j + (mas[a].j - mas[0].j), ship, player, without_move))
				num--
		}		
		if(num == 0)
			return false
		else
			return true
	}

	// Проверка ячейки на занятость
	function checkCell(i, j, ship, player, without_move) {
		if(i<0 || i>=dimension || j<0 || j>=dimension)
			return true
		var cur_obgects = _data.rocks
		// console.log(without_move)
		if(!without_move)
			cur_obgects = cur_obgects.concat(_data.used_move_user)
		cur_obgects = player == "user" ? cur_obgects.concat(selected_objects) : cur_obgects.concat(bot_selected_objects)
		for (var a = 0; a  < cur_obgects.length; a++) {
			var mas = cur_obgects[a].getTakes()
			if(cur_obgects[a] != ship)
				for (var b = 0; b < mas.length; b++) {
					if(mas[b].i == i && mas[b].j == j)
						return true
				};
		};
	}

	// Построение массива индекса проверяемых элементов (начиная с элемента i, j квадратная область размером num)
	function buildMasIndex(i, j, num){
		var mas = new Array()
		for(var a=0; a<num; a++)
			for(var b=0; b<num; b++){
				var p = [i + a, j + b]
				mas.push(p)
			}
		return mas
	}

	// Заполнение 
	function createFilling(i, j, ship){
		if(ship.getName() == "anim-target"){
			ship.setTakes(i, j)
		}else if(ship.getName() == "bomb"){
			ship.setTakes(i, j)
			ship.setTakes(i + 0, j + 1, true)
			ship.setTakes(i + 1, j + 0, true)
			ship.setTakes(i + 1, j + 1, true)

		}else if(ship.getName() == "dinamite"){
			var direct = ship.getDirection()
			ship.setTakes(i, j)
			if(direct == "goriz"){
				ship.setTakes(i + 0, j + 1, true)
				ship.setTakes(i + 0, j + 2, true)
			}else if(direct == "vert"){
				ship.setTakes(i + 1, j + 0, true)
				ship.setTakes(i + 2, j + 0, true)
			}
		}else if(ship.getName() == "periskop"){
			ship.setTakes(i, j)
			ship.setTakes(i + 0, j + 1, true)
			ship.setTakes(i + 1, j + 0, true)
			ship.setTakes(i + 1, j + 1, true)
		}else if(ship.getName() == "torpedo"){
			// console.log(ship.getTakes(), i, j)
			ship.setTakes(i, j)
			ship.setTakes(i + 0, j - 1, true)
			ship.setTakes(i + 1, j + 0, true)
			ship.setTakes(i - 1, j + 0, true)
			ship.setTakes(i + 0, j + 1, true)
			// console.log(ship.getTakes())
		}
	}
}