

--- DONT TOUCH ---
local HttpService = game:GetService("HttpService")
local indicator = script.Parent.housing.logo.Logo.Model.ThreeDTextObject.uni0069
local acct = script.Parent.accountNumber.Value
local data = script.Parent.Parent.Reporting
local GetName = game:GetService("MarketplaceService"):GetProductInfo(game.PlaceId)
local placeName = GetName.Name
local dialing = false
local sent = false
local fail = false

local url = "http://pbx.litenet.tel:3000/api/v1/webhook/kca/" .. acct

task.spawn(function()
	local state = false
	if not indicator then return end
	while true do
		wait()
		if not HttpService.HttpEnabled then
			indicator.Color = Color3.new(1,1,0)
			return
		end
		if dialing then
			if state then
				state = false
				indicator.Color = Color3.new(1, 1, 1)
				indicator.Material = Enum.Material.SmoothPlastic
				wait(.1)
			else
				state = true
				indicator.Color = Color3.new(0, 1, 1)
				indicator.Material = Enum.Material.Neon
				wait(.1)
			end
		elseif sent then
			indicator.Color = Color3.new(0,1,0)
			indicator.Material = Enum.Material.Neon
			wait(1)
			indicator.Color = Color3.new(1, 1, 1)
			indicator.Material = Enum.Material.SmoothPlastic
			sent = false
		elseif fail then
			indicator.Color = Color3.new(1,1,0)
			indicator.Material = Enum.Material.Neon
			wait(1)
			indicator.Color = Color3.new(1, 1, 1)
			indicator.Material = Enum.Material.SmoothPlastic
			sent = false
		end
	end
end)

function generateTransactionId()
	-- generate a 10 digit random number
	local transactionId = ""
	for i = 1, 10 do
		transactionId = transactionId .. tostring(math.random(0, 9))
	end
	return transactionId
end

-- Watch for updates to script.Parent.Parent.Reporting.armState.Value

script.Parent.Parent.Reporting.armState.Changed:Connect(function(val)
	if val == 3 then -- We've gone into alarm
		-- Prepare an HTTP POST request JSON data
		local postData = {
			["event"] = "alarm",
			["transaction"] = generateTransactionId(),
			["zoneNumber"] = data.alarmZone.Value,
			["zoneName"] = data.Parent.Zones[data.alarmZone.Value].zoneName.Value,
			["placeName"] = placeName,
			["systemName"] = data.Parent.Settings.SystemName.Value or "Your security system"
		}
		dialing = true
		local success, response = HttpService:PostAsync(url, HttpService:JSONEncode(postData), Enum.HttpContentType.ApplicationJson)
		if success then
			dialing = false
			sent = true
		else
			dialing = false
			fail = true
		end
	elseif val == 4 then -- fire
		-- Prepare an HTTP POST request JSON data
		local postData = {
			["accountNumber"] = acct,
			["event"] = "fire alarm",
			["transaction"] = generateTransactionId(),
			["zoneNumber"] = data.fireZone.Value,
			["zoneName"] = data.Parent.Zones[data.fireZone.Value].zoneName.Value,
			["placeName"] = placeName,
			["systemName"] = data.Parent.Settings.SystemName.Value or "Your security system"
		}
		dialing = true
		local success, response = HttpService:PostAsync(url, HttpService:JSONEncode(postData), Enum.HttpContentType.ApplicationJson)
		if success then
			dialing = false
			sent = true
		else
			dialing = false
			fail = true
		end
	end
end)