local OriginalHelp = CreateClientConVar("WM_OriginalHelpOnly", 0, true, false)
RunConsoleCommand("WM_IgnoreInvites", "0")
local SelectedShipType = 3
local opened = 0
 
local ShipSelectFrame
local DermaPanel

function ShipSelectMenu()
local xsize = 400
local ysize = 400
DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself
DermaPanel:SetPos( 25,25 ) -- Position on the players screen
DermaPanel:SetSize( xsize, ysize ) -- Size of the frame
DermaPanel:SetTitle( "Ship Selection" ) -- Title of the frame
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true ) -- Draggable by mouse?
DermaPanel:ShowCloseButton( true ) -- Show the close button?
DermaPanel:MakePopup() -- Show the frame
 

local PropertySheet = vgui.Create( "DPropertySheet" )
PropertySheet:SetParent( DermaPanel )
PropertySheet:SetPos( 5, 30 )
PropertySheet:SetSize( xsize - 20, ysize - 10 )

local Prop1 = vgui.Create( "DPanel" )
Prop1:SetParent( DermaPanel )
Prop1:SetPos( 0, 10 )
Prop1:SetSize( xsize - 30, ysize - 10 )
Prop1:SetName("Shit1")
local Prop2 = vgui.Create( "DPanel" )
Prop2:SetParent( DermaPanel )
Prop2:SetPos( 0, 10 )
Prop2:SetSize( xsize - 30, ysize - 10 )
Prop2:SetName("Shit2")

local Prop3 = vgui.Create( "DPanel" )
Prop3:SetParent( DermaPanel )
Prop3:SetPos( 0, 10 )
Prop3:SetSize( xsize - 30, ysize - 10 )
Prop3:SetName("Shit3")
PropertySheet:AddSheet( "Light", Prop1, "gui/silkicons/user", false, false, "Fighter sized" )
PropertySheet:AddSheet( "Medium",  Prop2, "gui/silkicons/wrench", false, false, "Destroyers and Cruisers" )
PropertySheet:AddSheet( "Heavy", Prop3, "gui/silkicons/world", false, false, "Battleships and such." )
PropertySheet.Think = function() 
	if Prop1:IsVisible() then
	SelectedShipType = 1
	elseif Prop2:IsVisible() then
	SelectedShipType = 2
	elseif Prop3:IsVisible() then
	SelectedShipType = 3
	end
	PropertySheet.animFade:Run()
end
opened = 0

local LightPanelList = vgui.Create("DListView",Prop1)
LightPanelList:SetPos(25, 30)
LightPanelList:SetSize(ScrW()*0.35, 240)
LightPanelList:SetMultiSelect(false)

local col1 = LightPanelList:AddColumn("Ship Name")
local col2 = LightPanelList:AddColumn("Description")
local col3 = LightPanelList:AddColumn("ID")
col1:PerformLayout()
col2:PerformLayout()
col3:SetFixedWidth(0)
col3:PerformLayout()
LightPanelList:SetSortable(true)
LightPanelList.OnClickLine = function(parent,selected,isselected)
SSPanelCall(selected:GetValue(1),selected:GetValue(3))
end  


local MidPanelList = vgui.Create("DListView",Prop2)
MidPanelList:SetPos(25, 30)
MidPanelList:SetSize(ScrW()*0.35, 240)
MidPanelList:SetMultiSelect(false)

local col1 = MidPanelList:AddColumn("Ship Name")
local col2 = MidPanelList:AddColumn("Description")
local col3 = MidPanelList:AddColumn("ID")
col1:PerformLayout()
col2:PerformLayout()
col3:SetFixedWidth(0)
col3:PerformLayout()
MidPanelList:SetSortable(true)
MidPanelList.OnClickLine = function(parent,selected,isselected)
SSPanelCall(selected:GetValue(1),selected:GetValue(3))
end

local HeavyPanelList = vgui.Create("DListView",Prop3)
HeavyPanelList:SetPos(25, 30)
HeavyPanelList:SetSize(ScrW()*0.35, 240)
HeavyPanelList:SetMultiSelect(false)

local col1 = HeavyPanelList:AddColumn("Ship Name")
local col2 = HeavyPanelList:AddColumn("Description")
local col3 = HeavyPanelList:AddColumn("ID")
col1:PerformLayout()
col2:PerformLayout()
col3:SetFixedWidth(0)
col3:PerformLayout()
HeavyPanelList:SetSortable(true)
HeavyPanelList.OnClickLine = function(parent,selected,isselected)
SSPanelCall(selected:GetValue(1),selected:GetValue(3))
end
--local scroller = vgui.Create("DHorizontalScroller", DermaPanel)
--scroller:AddPanel(MidPanelList)
	for k,v in pairs(ShipTypes[1]) do
	LightPanelList:AddLine(v["Name"], v["Description"], k)
	end
	for k,v in pairs(ShipTypes[2]) do
	MidPanelList:AddLine(v["Name"], v["Description"], k)
	end
	for k,v in pairs(ShipTypes[3]) do
	HeavyPanelList:AddLine(v["Name"], v["Description"], k)
	end

end


function SSPanelCall(Replacer, ID)
    if opened == 1 then
    xp,yp = ShipSelectFrame:GetPos()
    ShipSelectFrame:Close()
    ShipSelectFrame:Remove()
    end
SSPanel(Replacer,xp, yp, ID)
end

function SSPanel(Name, XPos, YPos,ID)
opened = 1
	if Name == nil then
	Name = "??"
	end


	if XPos == nil || YPos == nil then
	XPos = 25
	YPos = 425
	end

ShipSelectFrame = vgui.Create( "DFrame", DermaPanel ) -- Creates the frame itself
ShipSelectFrame:SetPos( XPos, YPos ) -- Position on the players screen
ShipSelectFrame:SetSize( 500, 300 ) -- Size of the frame
ShipSelectFrame:SetTitle( Name .. " Loadout")
ShipSelectFrame:SetVisible( true )
ShipSelectFrame:SetSizable(true)
ShipSelectFrame:SetDraggable( true ) -- Draggable by mouse?
ShipSelectFrame:ShowCloseButton( true ) -- Show the close button?
ShipSelectFrame:MakePopup() -- Show the frame

local CostLabel = vgui.Create("DLabel", ShipSelectFrame)
CostLabel:SetText("Cost: " .. ShipTypes[SelectedShipType][ID]["Cost"])
CostLabel:SetPos(10,25)
CostLabel:SizeToContents()
local HullStrengthLabel = vgui.Create("DLabel", ShipSelectFrame)
HullStrengthLabel:SetText("Hull Armor: " .. ShipTypes[SelectedShipType][ID]["Hull"])
HullStrengthLabel:SetPos(10,40)
HullStrengthLabel:SizeToContents()
local ShieldStrengthLabel = vgui.Create("DLabel", ShipSelectFrame)
ShieldStrengthLabel:SetText("Shield Strength: " .. ShipTypes[SelectedShipType][ID]["Shields"])
ShieldStrengthLabel:SetPos(10,55)
ShieldStrengthLabel:SizeToContents()
local ShieldRegenLabel = vgui.Create("DLabel", ShipSelectFrame)
ShieldRegenLabel:SetPos(10,70)
ShieldRegenLabel:SetText("Shield Regen: " .. ShipTypes[SelectedShipType][ID]["ShieldRegen"])
ShieldRegenLabel:SizeToContents()
local HardPointsLabel = vgui.Create("DLabel", ShipSelectFrame)
HardPointsLabel:SetPos(10,85)
HardPointsLabel:SetText("Hardpoints: " .. #ShipTypes[SelectedShipType][ID]["Hardpoints"])
HardPointsLabel:SizeToContents()
local CreditsLabel = vgui.Create("DLabel", ShipSelectFrame)
CreditsLabel:SetPos(10,100)
CreditsLabel:SetText("Your Credits: " .. credits)
CreditsLabel:SizeToContents()

local LastX = 150
local LastY = 25
HardpointChoice = {}
local HardpointLabels = {}
	for i=1, 10 do
	HardpointChoice[i] = 1
	HardpointLabels[i] = ""
	end
	
local HardpointMenus = {}

	for k,v in pairs (ShipTypes[SelectedShipType][ID]["Hardpoints"]) do
	LastY = LastY + 10
		if k == 6 || k == 12 then
		LastX = LastX + 150
		LastY = 35
		end
	HardpointLabels[k] = vgui.Create("DLabel", ShipSelectFrame)
	HardpointLabels[k]:SetPos(LastX, LastY)
	HardpointLabels[k]:SetText(ShipTypes[SelectedShipType][ID]["HardpointNames"][k])
	HardpointLabels[k]:SizeToContents()
	  HardpointMenus[k] = vgui.Create("DMultiChoice", ShipSelectFrame )
	  HardpointMenus[k]:SetText(ShipTypes[SelectedShipType][ID]["Hardpoints"][k])
	  HardpointMenus[k]:SetPos (LastX, LastY + 20)
	  HardpointMenus[k]:SetSize( 125, 20 )
	  LastY = LastY + 40
		for x, y in pairs (HardpointTypes[ShipTypes[SelectedShipType][ID]["Hardpoints"][k]]) do
		   HardpointMenus[k]:AddChoice(HardpointTypes[ShipTypes[SelectedShipType][ID]["Hardpoints"][k]][x][1])
		end
		HardpointChoice[k] = 1
		HardpointMenus[k].OnSelect = function(index,value,data)
		HardpointChoice[k] = value
		end
	  --HardpointMenus[k].OnSelect = function(index,value,data)
		--HardPointChoice[i] = HeavyShips[index]
	  --end
	end

local ConfirmButton = vgui.Create( "DButton", ShipSelectFrame )
ConfirmButton:SetText( "Confirm Loadout" )
ConfirmButton:SetPos( 15, 125 )
ConfirmButton:SetSize(125, 25)
ConfirmButton.DoClick = function()
    RunConsoleCommand( "LoadoutConfirmed", SelectedShipType, ID, HardpointChoice[1], HardpointChoice[2], HardpointChoice[3], HardpointChoice[4], HardpointChoice[5], HardpointChoice[6], HardpointChoice[7], HardpointChoice[8], HardpointChoice[9], HardpointChoice[10] )
    ShipSelectFrame:Remove()
	end
    
	
local seltex = "DMenus/BattleCruiser"
local seltexID = surface.GetTextureID( seltex )
	local ShipModelViewFrame = vgui.Create( "DFrame", ShipSelectFrame );
	ShipModelViewFrame:SetPos(ShipSelectFrame:GetWide()*0.9, 30)
	ShipModelViewFrame:SetSize( 425,400);
	ShipModelViewFrame:SetTitle( "Ship Preview" );
	ShipModelViewFrame:MakePopup();

	local ShipModelView = vgui.Create("DModelPanel", ShipModelViewFrame)
	ShipModelView:SetModel(ShipTypes[SelectedShipType][ID]["Model"])
	ShipModelView:SetPos(15,15)
	ShipModelView:SetSize(375,375)
	ShipModelView:SetCamPos(Vector(10,10,0) + SelectedShipType * Vector(75,75,75))
	ShipModelView:SetLookAt(Vector(0,0,0))
	
 	/*local ShipPicFrame = vgui.Create( "DFrame", ShipSelectFrame )
	ShipPicFrame:SetSize( ShipSelectFrame:GetWide() - 10, ShipSelectFrame:GetTall() + 100 )
	ShipPicFrame:SetPos( ShipSelectFrame:GetWide()*0.9, 30 )
	ShipPicFrame:SetTitle( "Ship Selection" ) -- Title of the frame
	ShipPicFrame:SetDraggable( true ) -- Draggable by mouse?
	ShipPicFrame:ShowCloseButton( true ) -- Show the close button?
	ShipPicFrame:MakePopup() -- Show the frame
	
	local ShipPic = vgui.Create("DImage", ShipPicFrame)
	ShipPic:SetPos(25, 25)
	ShipPic:SetImage("DMenus/BattleCruiser")
	ShipPic:SizeToContents()	--ShipPicFrame:SetImage("DMenus/BattleCruiser")
	--ShipPicFrame:SizeToContents()
--	html2:OpenURL("data/Dark_Skies/picturetemplate.txt")*/
	
--local PicPanel = vgui.Create( "DImage", ShipSelectFrame )
--PicPanel:SetPos( ShipSelectFrame:GetWide()*0.5, 5 )   
--PicPanel:SetSize( ShipSelectFrame:GetWide()*0.4, ShipSelectFrame:GetTall() - 30 )   
--PicPanel:SetImage("selectonemelon")
end



--SIList:AddItem(SI2)



local EOpened = 0
xp2,yp2 = 10, 310    
local EncyclopediaEntry

function Encyclopedia(ply, cmd, arg)
EOpened = 0
local title = ""
local seltex = "wmrtslogoAA5"
local seltexID = surface.GetTextureID( seltex )
local EntryID = 1
EncPanel = vgui.Create( "DFrame" ) -- Creates the frame itself
EncPanel:SetPos( 10, 10 ) -- Position on the players screen
EncPanel:SetSize( ScrW()*0.4, 300 ) -- Size of the frame
EncPanel:SetTitle( "WarMelons:RTS Encyclopedia (F2)" ) -- Title of the frame
EncPanel:SetDraggable( true ) -- Draggable by mouse?
EncPanel:ShowCloseButton( true ) -- Show the close button?
EncPanel:MakePopup() -- Show the frame

local EncPanelList = vgui.Create("DListView",EncPanel)
EncPanelList:SetPos(25, 30)
EncPanelList:SetSize(ScrW()*0.35, 240)
EncPanelList:SetMultiSelect(false)

--local col3 = vgui.Create("DListView_Column", EncPanelList)
--col3:SetName("Exp2lanation")
local col1 = EncPanelList:AddColumn("Topic")
local col2 = EncPanelList:AddColumn("Category")
local col3 = EncPanelList:AddColumn("ID")
col1:PerformLayout()
col2:PerformLayout()
col3:SetFixedWidth(0)
col3:PerformLayout()
EncPanelList:SetSortable(true)
EncPanelList.OnClickLine = function(parent,selected,isselected)
EEPanelCall(selected:GetValue(1))
end  
--local scroller = vgui.Create("DHorizontalScroller", DermaPanel)
--scroller:AddPanel(EncPanelList)

EncPanelList:AddLine("Railgun", "Weapons")
if arg[1] ~= nil then
EEPanelCall(arg[1])
end
end

function EEPanelCall(Replacer)
local Replacer2 = string.lower(Replacer)
local ini = file.Read("../data/Dark_Skies/help/picturetemplate.txt")
if file.Exists("../data/Dark_Skies/help/" .. Replacer2 .. ".jpg") then
file.Write("Dark_Skies/picturetemplate.txt", string.gsub(ini, "XXX", Replacer2))
else
file.Write("Dark_Skies/picturetemplate.txt", string.gsub(ini, "XXX", "wmrts"))
end
    if EOpened == 1 then
    xp2,yp2 = EncyclopediaEntry:GetPos()
    EncyclopediaEntry:Close()
    EncyclopediaEntry:Remove()
    end
EEPanel(Replacer,xp2, yp2)
end

function EEPanel(Name, XPos, YPos)
Eopened = 1
	if Name == nil then
	Name = "Getting Started"
	end


	if XPos == nil || YPos == nil then
	XPos = 10
	YPos = 310
	end

EncyclopediaEntry = vgui.Create( "DFrame", EncPanel ) -- Creates the frame itself
EncyclopediaEntry:SetPos( XPos, YPos ) -- Position on the players screen
EncyclopediaEntry:SetSize( ScrW()*0.9, ScrH()*0.5 ) -- Size of the frame
EncyclopediaEntry:SetTitle( Name )
EncyclopediaEntry:SetVisible( true )
EncyclopediaEntry:SetSizable(true)
EncyclopediaEntry:SetDraggable( true ) -- Draggable by mouse?
EncyclopediaEntry:ShowCloseButton( false ) -- Show the close button?
EncyclopediaEntry:MakePopup() -- Show the frame
local FinalLoc = ""
local Name2 = string.lower(Name)
    if OriginalHelp:GetFloat() == 0 then
    local tstring = "../data/Dark_Skies/help/*.txt"
    local list = file.Find(tstring)
    --PrintTable(list)
     for k, v in pairs(list) do
       if string.find(v, "]" .. Name2) ~= nil then
       --Msg("Found string")
            local date
            if date == nil then 
            --Msg("added a date")
           date = file.Time(tstring) 
           FinalLoc = v
           end
       --Msg(string.find(v, Name2))
       --Msg(v .. " ")
       tstring = "../data/Dark_Skies/help/" .. v
           if file.Time(tstring) > date then
           date = file.Time(tstring)
           FinalLoc = v
           end
        end
      end
    else
    FinalLoc =  "[0]" .. Name2 .. ".txt"
    end
	html = vgui.Create( "HTML", EncyclopediaEntry )
	html:SetSize( EncyclopediaEntry:GetWide()*0.5, EncyclopediaEntry:GetTall() - 40 )
	html:SetPos( 10, 30 )
	if file.Exists("../data/Dark_Skies/help/" .. FinalLoc) then
	html:SetHTML( file.Read("../data/Dark_Skies/help/" .. FinalLoc ) )
    else
    html:SetHTML( file.Read("../data/Dark_Skies/help/encyclopediaerror.txt" ) )
    end
  function html:OpeningURL( url, target ) 
   	 
 	local command = url:gsub( "lua://Entry/", "" ) 
 	command = string.Replace( command, "%20", " ")
 	if ( command ~= "" ) then 
 	 
 		EEPanelCall(command)
 		return true; 
 		 
 	end 
 	 
   end 

 	html2 = vgui.Create( "HTML", EncyclopediaEntry )
	html2:SetSize( EncyclopediaEntry:GetWide()*0.45 + 10, EncyclopediaEntry:GetTall() - 40 )
	html2:SetPos( EncyclopediaEntry:GetWide()*0.53, 30 )
	html2:SetHTML( file.Read("Dark_Skies/picturetemplate.txt") )
--	html2:OpenURL("data/Dark_Skies/picturetemplate.txt")
	
--local PicPanel = vgui.Create( "DImage", EncyclopediaEntry )
--PicPanel:SetPos( EncyclopediaEntry:GetWide()*0.5, 5 )   
--PicPanel:SetSize( EncyclopediaEntry:GetWide()*0.4, EncyclopediaEntry:GetTall() - 30 )   
--PicPanel:SetImage("selectonemelon")
end

function LocalPos()
local ent = LocalPlayer():GetEyeTraceNoCursor().Entity
print(LocalPlayer():GetEyeTraceNoCursor().Entity:WorldToLocal(LocalPlayer():GetEyeTraceNoCursor().HitPos))
end

function LocalAng()
local ent = LocalPlayer():GetEyeTraceNoCursor().Entity
print(ent:GetAngles())
end

function AimVec()
print(LocalPlayer():GetAimVector())
end
concommand.Add("ShipSelect", ShipSelectMenu);
concommand.Add("ShipSelect2", Encyclopedia);
concommand.Add("LocalPos", LocalPos)
concommand.Add("LocalAng", LocalAng)
concommand.Add("AimVec", AimVec)