local UiUtil = require 'Ui/UiUtil'
local SixthSenseRadio = require 'SixthSense'
local Monkey_ModData = require 'ModDataManager/Monkey_ModData';
local SixthSenseUiUtils = require "Ui/SixthSenseUiUtils";
local SixthSenseUtils = require "SixthSenseUtils";

SixthSenseUi = ISPanel:derive("SixthSenseUi");

local GRAY = {r=1, g=1, b=1, a=1};
local ORANGE = {r=1, g=0.647, b=0, a=1}
local YELLOW = {r=1, g=1, b=0, a=1}
local RED = {r=1, g=0, b=0, a=1}

local OSCILLATION_DELAY = 12;
local RADIO_BUTTON_X = 11

function SixthSenseUi:render()
	if self:isMouseOver() then
		self:drawRect(self:getWidth() - 12, 0, 12, 12, 0.5, 1, 1, 1);
	end

	if self.senseRemaining < SixthSenseUtils:RemainingSenseMinimum() then
		UiUtil.drawOutlinedBar(self, 8, 50, 2, 38, 6, self.senseRemaining, {r=1,g=0,b=0.0,a=1});
	else
		UiUtil.drawOutlinedBar(self, 8, 50, 2, 38, 6, self.senseRemaining, {r=0.6,g=1,b=0.6,a=1});
	end
	if self.active then
        UiUtil.updateOscillation(self.radioButton);
    else
        self.radioButton:setX(RADIO_BUTTON_X);
        self.radioButton.oscillationData.delta = 0;
	end
end

function SixthSenseUi:onOptionMouseDown(button, x, y)
end

function SixthSenseUi:onMouseUp(x,y)
	if self.moving and self.playerNum == 0 then
		SixthSenseUiUtils.saveUiOffsets(self.x, self.y, self.modData);
		SixthSense_UiController.applyUiOffsets();
	end
	ISPanel.onMouseUp(self, x, y);
end

function SixthSenseUi:updateSenseImage()
	if not self.active then
		self.radioButton:setImage(self.senseInactiveTex);
	else
		self.radioButton:setImage(self.senseActiveTex);
	end
end

function SixthSenseUi:updateSenseInfo(active, senseRemaining, intensity)
	self.senseRemaining = senseRemaining;
	self.intensity = intensity;
	self.active = active;
	local col;
	if self.active and intensity > 0 then
		col = UiUtil.triLerpColors(intensity or 0, YELLOW, ORANGE, RED);
	else
		col = Color.new(GRAY.r, GRAY.g, GRAY.b, GRAY.a);
	end

	self.radioBg:setColor(col.r, col.g, col.b);
end

function SixthSenseUi:new(x, y, playerNum)
	local o = {}
	o = ISPanel:new(x, y, 64, 64);
	setmetatable(o, self)
    self.__index = self
	o.x = x;
	o.y = y;
    o.borderColor = {r=0.4, g=0.4, b=0.4, a=0};
    o.backgroundColor = {r=0, g=0, b=0, a=0};
    o.anchorLeft = true;
    o.playerNum = playerNum;
	o.anchorRight = false;
	o.anchorTop = true;
	o.anchorBottom = false;
	o.moveWithMouse = true;
	
	o.backgroundTex = getTexture("media/ui/HandMain2_Off.png");
	o.senseActiveTex = getTexture("media/ui/SixthSenseUi/sense-active.png");
	o.senseInactiveTex = getTexture("media/ui/SixthSenseUi/sense-inactive.png");

	o.senseRemaining = 0.0;
	o.intensity = 0.0;
    o.oscillationTick = 0;

	o.modData = Monkey_ModData:initEntityModData(getPlayer(), 'SixthSense');

    SixthSenseUi.instance = o;
	return o;
end

function SixthSenseUi:destroyUi()
    self:clearChildren();
    self:close();
    self:removeFromUIManager();
end

function SixthSenseUi:initialise()
	ISPanel.initialise(self);

    self.radioBg = ISImage:new(4, 0, self.backgroundTex:getWidthOrig(), self.backgroundTex:getHeightOrig(), self.backgroundTex);
    self.radioBg:initialise();
    self.radioBg.parent = self;
    self:addChild(self.radioBg);

	self.radioButton = ISButton:new(RADIO_BUTTON_X, 7, 32, 32, "", self, SixthSenseUi.onOptionMouseDown);
    self.radioButton:setImage(self.backgroundTex);
	self.radioButton:setOnClick(SixthSenseUtils.ToggleSixthSense);
    self.radioButton.internal = "SIXTHSENSE";
    self.radioButton:initialise();
    self.radioButton:instantiate();
    self.radioButton:setDisplayBackground(false);

    local oscillateSense = function() 
            return self.senseRemaining <= 0.2; 
        end;

    UiUtil.setupOscillation(self.radioButton, RADIO_BUTTON_X, OSCILLATION_DELAY, oscillateSense);

    self.radioButton.borderColor = {r=1, g=1, b=1, a=0.1};
    self.radioButton:ignoreWidthChange();
    self.radioButton:ignoreHeightChange();
	self:addChild(self.radioButton); 
end