require "class"
require "screens/pausescreen"
local easing = require "easing"

local trace = function() end

local CLICK_WALK_TIME = .5

local prefabs =
{
}


local playercontroller_camera = Class(function(self, inst)
    self.inst = inst
    self.enabled = true
    	
	self.camfocus = 0
	self.playerfocus = 0

	
    self.inputhandlers = {}

	
	self.time = 0	
	
	table.insert(self.inputhandlers, TheInput:AddKeyDownHandler(KEY_X, function() self:SpawnPlayer2() end))
	table.insert(self.inputhandlers, TheInput:AddKeyDownHandler(KEY_KP_MINUS, function() self:SpawnPlayer2() end))
	
	
	
    table.insert(self.inputhandlers, TheInput:AddKeyUpHandler(KEY_Z, function() self:CameraFocus() end))---7
    self.inst:StartUpdatingComponent(self)
    self.draggingonground = false
    self.startdragtestpos = nil
    self.startdragtime = nil
	
end)



function playercontroller_camera:CameraFocus()
    self.camfocus = 0
    
    if self.playerfocus == 0 then
        self.playerfocus = 1
    elseif self.playerfocus == 1 then
        self.playerfocus = 0
    else
        self.playerfocus = 0
    end
    
end

function playercontroller_camera:SpawnPlayer2()
	self.inst:DoTaskInTime(1, function() 
	
    self.inst.components.health:DoDelta(900000)
    self.camfocus = 1
	
	end)
end




function playercontroller_camera:OnUpdate(dt)
    local player1 = GetPlayer()
   
	local testpl2 = player1.components.playercontroller_spawner:FindPlayerByNum(2, true) or player1--FindEntity(self.i
    local testpl3 = player1.components.playercontroller_spawner:FindPlayerByNum(3, true) or player1--FindEntity(self.inst, 100, function(guy) return (guy.components.playercontroller_3)end)
    
	local player2 = player1
	local camdist12 = 1
	local camdist13 = 1
	if player1 ~= testpl2 then
		camdist12 = player1:GetDistanceSqToInst(testpl2)
	end
	if player1 ~= testpl3 then
		camdist12 = player1:GetDistanceSqToInst(testpl3)
	end		
	--local camdist23 = 
	if camdist12 > camdist13 then
		player2 = testpl2
	elseif camdist12 < camdist13 then
		player2 = testpl3
	else
		player2 = nil
	end
	
	--if not player2 then player2 = player1.components.playercontroller_spawner:FindPlayerByNum(3, true) end
    if player2 then
        if self.camfocus == 1 then
            local pt = player1:GetPosition()
            local pt2 = player2:GetPosition()
        
            self.inst.Transform:SetPosition(pt.x, pt.y, pt.z)
            TheCamera:SetTarget(player1)
            TheCamera:SetDistance(35)
            local posx = (pt.x + pt2.x)*0.5
           -- local posy = (pt.y + pt2.y)*0.5
            local posz = (pt.z + pt2.z)*0.5
        
            local dist = player1:GetDistanceSqToInst(player2)*0.1
			--local dist2 = player1:GetDistanceSqToInst(player2)
            if dist < 35 then
                dist = 35
            end
        
            if dist > TUNING.COOPMAXCAMHEIGHT then
                dist = TUNING.COOPMAXCAMHEIGHT
            end
        
            local distf = (dist^-1)*14
            TheCamera:SetDistance(((dist*1.1)+distf))
            TheCamera:SetTarget(self.inst)
            self.inst.Transform:SetPosition(posx, 0, posz)
        --  if player1:GetDistanceSqToInst(player2)>0.75*TUNING.MAXDIST then
				--[[--player2:AddTag("moveblocked")
			--	print("playercontroller_3 == DISABLED")
                local pt = player1:GetPosition()
				if (player1.components.driver and player1.components.driver.driving ==false)
				and (player2.components.driver and player2.components.driver.driving ==true) then
					player2.components.playercontroller_3:UnMountBoat(true)
					--player2.components.driver:OnDismount(nil, player2:GetPosition())
					self.time =  self.time +dt
					print("107 time =  "..self.time)
				end
				self.time =  self.time +dt
				--print("110 time =  "..self.time)
				--if 
			--	if self.time>6 then
				--	player2.Transform:SetPosition(posx, posy, posz)
				--	self.time = 0
				--end
			elseif  self.time>0 then
				self.time = 0
				--player2:RemoveTag("moveblocked")
            end--]]
        else
            if self.playerfocus == 0 then
                local pt = player1:GetPosition()
                self.inst.Transform:SetPosition(pt.x, pt.y, pt.z)
                TheCamera:SetTarget(player1)
                TheCamera:SetDistance(35)
            elseif self.playerfocus == 1 then
                local pt = player1:GetPosition()
                self.inst.Transform:SetPosition(pt.x, pt.y, pt.z)
                TheCamera:SetTarget(player1)
                TheCamera:SetDistance(35)
            else
                self.playerfocus = 0
            end
        end
    else
        if (self.inst:GetDistanceSqToInst(player1) > (1*1)) then
            local pt = player1:GetPosition()
            self.inst.Transform:SetPosition(pt.x, pt.y, pt.z)
            TheCamera:SetTarget(player1)
            TheCamera:SetDistance(35)
        end
    end
end



return playercontroller_camera
