local cond_cp_result= GAMESTATE:IsPlayerEnabled(PLAYER_1)

local cond_dp_result= GAMESTATE:IsPlayerEnabled(PLAYER_2)

local function optional_actor(cond, actor)
	if cond then return actor end
	return Def.Actor{}
end

return Def.ActorFrame{
  Name= "actor_ep",
  Def.Quad{
    Name= "actor_fp",
    InitCommand= function(self)
		mawaru_chike_bars = {}
		
		function mawaru_chike_nf(pn)
			if mawaru_curgame == 9 and GAMESTATE:IsPlayerEnabled(pn-1) then
				if mawaru_chike_lives[pn] > 1 then
					mawaru_chike_lives[pn] = mawaru_chike_lives[pn]-1
					Sound('ddrsuck');
				elseif mawaru_chike_lives[pn] == 1 then
					mawaru_chike_lives[pn] = 0
					local a = _G['P'..pn]
					if a then
						a:linear(0.35);
						a:rotationz(360);
						a:zoom(0);
						a:sleep(0);
						a:rotationz(0);
					end
					MESSAGEMAN:Broadcast('ChikeGameOverP'..pn);
					Sound('ddrfail');
				end
			end
		end
		
		mawaru_chike_scores = {'',''}
		mawaru_chike_score = {0,0}
		mawaru_chike_scoreprev = {0,0}
		
		mawaru_chike_maxlives = {8,8}
		mawaru_chike_lives = {0,0}
		
	end,
    OnCommand= cmd(visible,false),
    StartCommand= function(self)
	
		mawaru_chike_maxlives = {8,8}
		mawaru_chike_lives = {0,0}
		mawaru_chike_scroller = 0;
		
		for pn=1,2 do
			if GAMESTATE:IsPlayerEnabled(pn-1) then
				if mawaru_difficulty[pn] > 3 then
					mawaru_chike_maxlives[pn] = 4;
					MESSAGEMAN:Broadcast('ChikeFrame4P'..pn);
				else
					MESSAGEMAN:Broadcast('ChikeFrame8P'..pn);
				end
				mawaru_chike_lives[pn] = mawaru_chike_maxlives[pn]
				if mawaru_difficulty[pn] > 4 then
					mawaru_chike_lives[pn] = 2
				end
			end
		end
		
		self:queuecommand('Update');
	end,
    UpdateCommand= function(self)
	
		for pn=1,2 do
			if GAMESTATE:IsPlayerEnabled(pn-1) then
				local m = mawaru_chike_scroller;
				mawaru_chike_scroller = mawaru_chike_scroller+0.02;
				if mawaru_chike_scroller > 1 then
					mawaru_chike_scroller = mawaru_chike_scroller-1
				end
				if mawaru_chike_lives[pn] == mawaru_chike_maxlives[pn] then
					mawaru_chike_bars[pn]:customtexturerect(m,.5,m+1,.75);
				elseif mawaru_chike_lives[pn] > 1 then
					mawaru_chike_bars[pn]:customtexturerect(m,.25,m+1,.5);
				else
					mawaru_chike_bars[pn]:customtexturerect(m,0,m+1,.25);
				end
				if pn == 1 then
					mawaru_chike_bars[pn]:cropright(1-(mawaru_chike_lives[pn]/mawaru_chike_maxlives[pn]))
				else
					mawaru_chike_bars[pn]:cropleft(1-(mawaru_chike_lives[pn]/mawaru_chike_maxlives[pn]))
				end
				--Trace((mawaru_chike_lives[pn]/mawaru_chike_maxlives[pn]))
				
				--We can just replace this with JudgmentMessageCommand! -Kid
-- 				if not FUCK_EXE then
-- 					mawaru_chike_scores[pn] = SCREENMAN:GetTopScreen():GetChild('ScoreP'..pn):GetChild('ScoreDisplayPercentage Percent'):GetChild('PercentP'..pn):GetText();
-- 					mawaru_chike_score[pn] = tonumber(string.sub(mawaru_chike_scores[pn],1,string.len(mawaru_chike_scores[pn]) - 1) * 100);
-- 					
-- 					if mawaru_chike_score[pn] < mawaru_chike_scoreprev[pn] then
-- 						mawaru_chike_nf(pn)
-- 					end
-- 					
-- 					mawaru_chike_scoreprev[pn] = mawaru_chike_score[pn];
-- 				end
				
			else
				mawaru_chike_bars[pn]:visible(false);
			end
		end
		
		
	
		if mawaru_curgame == 9 then
			self:sleep(0.02)
			self:queuecommand('Update');
		end
	end,
  },
  --For all graphical parts, Unconverted x & y pos. -Kid
  Def.Sprite{
    Name= "actor_gp",
    OnCommand= cmd(x,320;y,240),
    Texture= "bg.png",
  },
  Def.Sprite{
    Name= "actor_hp",
    OnCommand= cmd(customtexturerect,0,0,1,.25;horizalign,left;vertalign,top;zoomto,228,16;x,26;y,10+20;sleep,0.02;queuecommand,"SetMe"),
    SetMeCommand= function(self) table.insert(mawaru_chike_bars,self) end,
    Texture= "bar.png",
  },
  Def.Sprite{
    Name= "actor_ip",
    OnCommand= cmd(customtexturerect,0,0,1,.25;horizalign,right;vertalign,top;zoomto,228,16;x,640-26;y,10+20;sleep,0.02;queuecommand,"SetMe"),
    SetMeCommand= function(self) table.insert(mawaru_chike_bars,self) end,
    Texture= "bar.png",
  },
  optional_actor(cond_cp_result,
  Def.Sprite{
    Name= "actor_jp",
    ChikeFrame4P1MessageCommand= cmd(setstate,0),
    ChikeFrame8P1MessageCommand= cmd(setstate,1),
    OnCommand= cmd(horizalign,left;vertalign,top;animate,0;x,0;y,0+20),
    Texture= "frame 1x2.png",
  }),
  optional_actor(cond_dp_result,
  Def.Sprite{
    Name= "actor_kp",
    ChikeFrame4P2MessageCommand= cmd(setstate,0),
    ChikeFrame8P2MessageCommand= cmd(setstate,1),
    OnCommand= cmd(horizalign,left;vertalign,top;animate,0;x,640;y,0+20;zoomx,-1),
    Texture= "frame 1x2.png",
  }),
  Def.ActorFrame{
    Name= "actor_lp",
    ChikeGameOverP1MessageCommand= cmd(bounceend,0.2;zoomy,1),
    OnCommand= cmd(x,170;y,39;basezoomx,0.8;basezoomy,0.8;bob;effectperiod,0.6;effectmagnitude,0,3,0;zoomy,0),
    Def.Sprite{
      Name= "actor_mp",
      OnCommand= cmd(zoomy,1;rotationz,-6;diffuseshift;effectperiod,0.4;effectcolor1,color('1,0,0,1');effectcolor2,color('0,1,0,1')),
      Texture= "gameover2.png",
    },
  },
  Def.ActorFrame{
    Name= "actor_np",
    ChikeGameOverP2MessageCommand= cmd(bounceend,0.2;zoomy,1),
    OnCommand= cmd(x,470;y,39;basezoomx,0.8;basezoomy,0.8;bob;effectperiod,0.6;effectmagnitude,0,3,0;zoomy,0),
    Def.Sprite{
      Name= "actor_op",
      OnCommand= cmd(zoomy,1;rotationz,-6;diffuseshift;effectperiod,0.4;effectcolor1,color('1,0,0,1');effectcolor2,color('0,1,0,1')),
      Texture= "gameover2.png",
    },
  },
  Def.Sprite{
    Name= "actor_pp",
    ChikeGameOverP1MessageCommand= cmd(linear,0.2;zoom,1;rotationz,352),
    OnCommand= cmd(x,160;y,230;zoom,0;rotationz,-8;bob;effectperiod,0.5;effectmagnitude,0,5,0),
    Texture= "gameover1.png",
  },
  Def.Sprite{
    Name= "actor_qp",
    ChikeGameOverP2MessageCommand= cmd(linear,0.2;zoom,1;rotationz,352),
    OnCommand= cmd(x,480;y,230;zoom,0;rotationz,-8;bob;effectperiod,0.5;effectmagnitude,0,5,0),
    Texture= "gameover1.png",
  },
  Def.Quad{
    Name= "actor_rp",
    ChikeGameOverP1MessageCommand= cmd(sleep,0.4;queuecommand,"Disable"),
    DisableCommand= function(self) if FUCK_EXE then GAMESTATE:UnloadSteps(0) end end,
  },
  Def.Quad{
    Name= "actor_sp",
    ChikeGameOverP2MessageCommand= cmd(sleep,0.4;queuecommand,"Disable"),
    DisableCommand= function(self) if FUCK_EXE then GAMESTATE:UnloadSteps(1) end end,
  },
  Def.Quad{
    Name= "actor_tp",
--     Fk_P1_W2MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(1); end end,
--     Fk_P1_W3MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(1); end end,
--     Fk_P1_W4MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(1); end end,
--     Fk_P1_W5MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(1); end end,
--     Fk_P1_W6MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(1); end end,
--     Fk_P2_W2MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(2); end end,
--     Fk_P2_W3MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(2); end end,
--     Fk_P2_W4MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(2); end end,
--     Fk_P2_W5MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(2); end end,
--     Fk_P2_W6MessageCommand= function(self) if mawaru_gamestate == 1 then mawaru_chike_nf(2); end end,
    OnCommand= cmd(visible,false),
    JudgmentMessageCommand=function(self,params)--Taken from TaroNuke's Louder Louder SM5 file, modded to fit here. -Kid
			if mawaru_gamestate == 1 then
				for pn=1,2 do
					if params.Player == 'PlayerNumber_P'..pn then
						--MESSAGEMAN:Broadcast(ToEnumShortString(params.TapNoteScore)..'P'..pn)
						--Better hope nothing has a AvoidMine or HitMine message command. -Kid
						if params.TapNoteScore == 'TapNoteScore_W2'
						or params.TapNoteScore == 'TapNoteScore_W3'
						or params.TapNoteScore == 'TapNoteScore_W4'
						or params.TapNoteScore == 'TapNoteScore_W5'
						or params.TapNoteScore == 'TapNoteScore_Miss' then
						    mawaru_chike_nf(pn)
						end
					end
				end
			end
		end,
  },
}
