-- This file is part of thesixthroc's Pirate Ship softmod, licensed under GPLv3 and stored at https://github.com/ComfyFactory/ComfyFactorio and https://github.com/danielmartin0/ComfyFactorio-Pirates.

local Memory = require('maps.pirates.memory')
local Math = require('maps.pirates.math')
-- local Balance = require 'maps.pirates.balance'
-- local Structures = require 'maps.pirates.structures.structures'
local Boats = require('maps.pirates.structures.boats.boats')
local Common = require('maps.pirates.common')
local Hold = require('maps.pirates.surfaces.hold')
local Cabin = require('maps.pirates.surfaces.cabin')
local CoreData = require('maps.pirates.coredata')
local Utils = require('maps.pirates.utils_local')
local _inspect = require('utils.inspect').inspect

local Public = {}

Public.Data = {}
Public.Data.display_names = { { 'pirates.location_displayname_dock_1' } }
Public.Data.discord_emoji = CoreData.comfy_emojis.hype
Public.Data.width = 296
Public.Data.height = 98

Public.Data.static_boat_bottom = 34
Public.Data.player_boat_top = -29
Public.Data.playerboat_starting_xcoord = 10
Public.Data.markets_position = { x = 6.5, y = -46.5 }
Public.Data.rightmostgate_stopping_xposition = 16

Public.Data.static_params_default = {
	starting_time_of_day = 0,
	daynightcycletype = 1,
	width = Public.Data.width,
	height = Public.Data.height,
}

Public.PurchaseableBoats = {
	[Boats.enum.SLOOP] = {
		type = Boats.enum.SLOOP,
		position = Utils.snap_coordinates_for_rails({
			x = -23 - Boats[Boats.enum.SLOOP].Data.leftmost_gate_position,
			y = Public.Data.static_boat_bottom - Boats[Boats.enum.SLOOP].Data.height / 2,
		}),
	},
	-- [Boats.enum.CUTTER] = {
	-- 	type = Boats.enum.CUTTER,
	-- 	position = Utils.snap_coordinates_for_rails({x = 24 - Boats[Boats.enum.CUTTER].Data.leftmost_gate_position, y = Public.Data.static_boat_bottom + Boats[Boats.enum.CUTTER].Data.height/2}),
	-- 	cannonscount = 4
	-- },
	-- [Boats.enum.CUTTER_WITH_HOLD] = {
	-- 	type = Boats.enum.CUTTER_WITH_HOLD,
	-- 	position = Utils.snap_coordinates_for_rails({x = 24 - Boats[Boats.enum.CUTTER_WITH_HOLD].Data.leftmost_gate_position, y = Public.Data.static_boat_bottom + Boats[Boats.enum.CUTTER_WITH_HOLD].Data.height/2}),
	-- 	cannonscount = 4
	-- },
	-- [Boats.enum.SLOOP_WITH_HOLD] = {
	-- 	type = Boats.enum.SLOOP_WITH_HOLD,
	-- 	position = Utils.snap_coordinates_for_rails({x = 24 - Boats[Boats.enum.SLOOP_WITH_HOLD].Data.leftmost_gate_position, y = Public.Data.static_boat_bottom + Boats[Boats.enum.SLOOP_WITH_HOLD].Data.height/2}),
	-- 	cannonscount = 2
	-- },
}

Public.Data.iconized_map_width = 4
Public.Data.iconized_map_height = 20

function Public.execute_boat_purchase()
	local memory = Memory.get_crew_memory()
	local destination = Common.current_destination()

	memory.boat = destination.dynamic_data.boat_for_sale
	destination.dynamic_data.boat_for_sale = nil

	Hold.connect_up_linked_belts_to_deck()
	Cabin.connect_up_linked_belts_to_deck()
end

function Public.place_dock_jetty_and_boats()
	local memory = Memory.get_crew_memory()
	local boat = memory.boat
	local destination = Common.current_destination()
	if not (boat and boat.surface_name) then
		return
	end
	local surface = game.surfaces[boat.surface_name]

	local tiles = {}

	Common.add_tiles_from_blueprint(tiles, Public.Data.ground_bp_1, 'grass-4', Public.Data.ground_bp_1_offset)
	Common.add_tiles_from_blueprint(tiles, Public.Data.jetty_bp, CoreData.walkway_tile, Public.Data.jetty_offset)
	Common.add_tiles_from_blueprint(tiles, Public.Data.stone_bp_1, 'stone-path', Public.Data.stone_bp_1_offset)

	surface.set_tiles(tiles, true)

	local boat_for_sale_type = destination.static_params.boat_for_sale_type
	if boat_for_sale_type then
		local boat2 = Utils.deepcopy(Public.PurchaseableBoats[boat_for_sale_type])

		-- not needed whilst we're not buying boats:
		-- boat2.dockedposition = boat2.position
		-- boat2.state = Boats.enum_state.DOCKED
		-- boat2.speed = 0
		-- boat2.decksteeringchests = {}
		-- boat2.questrewardchest = nil
		-- boat2.hold_input_belts = boat.hold_input_belts
		-- boat2.hold_output_belts = boat.hold_output_belts

		-- boat2.crows_nest_steering_chests = boat.crows_nest_steering_chests
		-- boat2.cannons = {}
		-- boat2.speedticker1 = 0
		-- boat2.speedticker2 = 1/3 * Common.boat_steps_at_a_time
		-- boat2.speedticker3 = 2/3 * Common.boat_steps_at_a_time

		boat2.force_name = boat.force_name
		boat2.surface_name = boat.surface_name
		Boats.place_boat(boat2, CoreData.static_boat_floor, true, true)

		-- Boats.deck_place_random_obstacle_boxes(boat2, 6, {}, 2)

		destination.dynamic_data.boat_for_sale = boat2
	end

	-- for y = -3.5, 3.5 do
	-- 	local e = surface.create_entity{name = 'stone-wall', position = {x = -68.5, y = y}, force = 'environment'}
	-- e.destructible = false
	-- e.minable = false
	-- e.rotatable = false
	-- e.operable = false
	-- end
end

function Public.terrain(args)
	-- local x, y = args.p.x, args.p.y

	args.tiles[#args.tiles + 1] = { name = 'water', position = args.p }
	if Math.random(165) == 1 then
		args.entities[#args.entities + 1] = { name = 'fish', position = args.p }
	end
end

function Public.chunk_structures()
	return nil
end

Public.Data.jetty_offset = { x = -20, y = -38 }
Public.Data.jetty_bp =
	[[0eNqVmcFO4zAYhN/F5yBl5ncSJ6+y4lAgQpFKWrUBgVDenZZw4LC76nesNHVGHs/nxP5MD/vX8Xia5iUNn+k87453y+Hu+TQ9XX+/pyFylT7SkNu1StPjYT6n4c9FOD3Pu/1VsnwcxzSkaRlfUpXm3cv110X3eBqXMV3/ND+Nl3G03ldpmfbjNsDxcJ6W6TD/PKXeHhLr34b4lzojdXObWsiJkBMhJ0ZOjJwYOQnkJJCTQE4ycpKRk4ycNMhJg5w0yEmLnLTISYucdMhJh5x0yElBTgpyUpCTHjnpkZOekY1BVoyygpiFnIWgZaQVQ60Ya8VgK0ZbMdyK8VYMuGLEFUOuGHPFoCtGXTHsinFXDLxi5BVDrxh7xeArRl8x/IrxVwzAYgQ2I7AZgc0IbEZgMwIbvuvCl134tssIbEZgMwKbEdiMwGYENiOwGYHNCGxGYDMCmxHYjMDeGNkidYfUBakDynskzzWTB5NnJr81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ToI0MfGfrI0EeGPjJ92QV9ZOgj07dp9HUafZ9GX6hBHxn6yNBHhj4y9JGhjwx9ZOgjQx8ZesfQO4beMfSOoXcMPWLoEUOP+L8eeai+fjEy/PHzkyr9HE/nzxPURd32anMdztt8ufwGAFgVOg==]]

Public.Data.ground_bp_1_offset = { x = -122, y = -49 }
Public.Data.ground_bp_1 =
	[[[encoded data, 9109 characters]]]

return Public
