-- Hackling Rake - Original macro by Bardoth (T6) - Revised by Cegaiel
-- Runs the Hacking Rake (or Flax Comb). Monitors the skills tab and only clicks when its all black.
--
--

dofile("common.inc");

do_click_refresh = 1;
do_click_refresh_when_end_red = 1;
improved_rake = 0;
num_flax = 0;
num_loops = 0;
per_rake = 10;
repairAttempt = 0;


function promptRakeNumbers()
	scale = 0.8;
	local z = 0;
	local is_done = nil;
	local value = nil;
	-- Edit box and text display
	while not is_done do
		-- Put these everywhere to make sure we don't lock up with no easy way to escape!
		checkBreak("disallow pause");
		
		lsPrint(10, 10, z, scale, scale, 0xFFFFFFff, "Hackling Raking Setup");
		
		-- lsEditBox needs a key to uniquely name this edit box
		--   let's just use the prompt!
		-- lsEditBox returns two different things (a state and a value)
		local y = 40;
		lsPrint(5, y, z, scale, scale, 0xFFFFFFff, "How much flax: ");
		is_done, num_flax = lsEditBox("passes",
			160, y, z, 70, 30, scale, scale,
			0x000000ff, 1);
		if not tonumber(num_flax) then
			is_done = nil;
			lsPrint(5, y+18, z+10, 0.7, 0.7, 0xFF2020ff, "MUST BE A NUMBER");
			num_flax = 1;
		end
		y = y + 50;
		improved_rake = CheckBox(10, y, z, 0xFFFFFFff, " Improved Rake", improved_rake);
		lsSetCamera(0,0,lsScreenX/1.0,lsScreenY/1.0);
		
		if improved_rake then
			per_rake = 30;
		else
			per_rake = 10;
		end
		
		num_loops = math.floor(num_flax / per_rake);
			if num_loops == 0 then
				num_loops = 1;
			end
		y = y + 32;

		if lsButtonText(10, lsScreenY - 30, z, 100, 0xFFFFFFff, "Next") then
			is_done = 1;
		end

		lsPrintWrapped(10, y, z+10, lsScreenX - 20, 0.7, 0.7, 0xD0D0D0ff, "This will attempt to rake " .. num_flax .. " rotten flax, requiring " .. num_loops .. " cycles.");

		if is_done and (not num_flax) then
			error 'Canceled';
		end
		
		if lsButtonText(lsScreenX - 110, lsScreenY - 30, z, 100, 0xFFFFFFff, "End script") then
			error "Clicked End Script button";
		end
	
		lsDoFrame();
		lsSleep(10); -- Sleep just so we don't eat up all the CPU for no reason
	end
end


function doit()
	promptRakeNumbers();
	askForWindow("Hackling Rake v1.0 by Bardoth - (Revised by Cegaiel)\n\nPin Hacking Rake or Flax Comb window up and have Rotten Flax in your inventory.\n\nMake sure your rake is showing \"Step 1, Remove Straw\" before starting.\n\nYou MUST have Skills window open and everything from Strength to Perception skill should be visible.\n\nYou can optionally pin 'Eat some Grilled Onion' menu to eat it whenever your endurance is not green.");

	step = 1;
	local task = "";
	local task_text = "";
	local warn_small_font=nil;
	local warn_large_font=nil;
	local loop_count=1;
	local straw = 0;
	local tow = 0;
	local lint = 0;
	local clean = 0;
	local eatTimer = 0;
	local startTime = lsGetTimer();

	checkCurrentStep(); -- Verify what step we're on when you start macro and update.
        if step > 1 then
          delay_loop_count = 1; --If we're starting in the middle of a previous session, then don't advance loop_count (later). Finish up what's already being processed.
	  end
	
	while num_loops do
		runTimer = lsGetTimer();
		srReadScreen();
		grilledOnion = findText("Grilled Onions");
		buffed = srFindImage("foodBuff.png");
		OK = srFindImage("ok.png"); -- If we got an OK popup, this suggests "Your Flax Comb/Hackling Rake has wore out", quit
		foundRepair = findText("Repair");

		stats_black2 = nil;
		stats_black3 = nil;
		stats_blackB = nil;
		stats_blackC = nil;
		
		stats_black = srFindImage("endurance.png");
		stats_blackB = srFindImage("endurance2.png"); -- We can proceed when it's semi-dark red (same as white)
		stats_blackC = srFindImage("endurance3.png"); -- We can proceed when it's dark red (same as white)

		-- Prevent eating more than 1 grilled per 10m, in case the screen didn't detect foodbuff
		if grilledOnion and not buffed  and (eatTimer == 0 or runTimer - eatTimer > 600000) then
		  clickAllText("Grilled Onions");
                  eatTimer = lsGetTimer()
		end

		if not stats_black then
			--stats_black2 = srFindImage("AllStats-Black2.png");
			if not stats_black2 then
				--stats_black3 = srFindImage("AllStats-Black3.png");
				if stats_black3 then
					warn_large_font = true;
				end
			else
				warn_small_font = true;

			end
		end
		
		local warning="";
		if warn_small_font then
			warning = "Your font size appears to be smaller than the default, many macros here will not work correctly.";
		elseif warn_large_font then
			warning = "Your font size appears to be larger than the default, many macros here will not work correctly.";
		end

		if step == 1 then
			task = "Separate Rotten Flax";
			task_text = "Remove Straw";
		elseif step == 2 then
			task = "Continue processing Rotten";
			task_text = "Separate Tow";
		elseif step == 3 then
			task = "Continue processing";
			task_text = "Refine the Lint";
		elseif step == 4 then
			task = "Clean the";
			task_text = "Clean the Rake";
		end
		

		if eatTimer > 0 and (runTimer - eatTimer > 720000) then
		  eatTimer = 0;
		  lastAte = "\nAte Last Grilled: 12m has elapsed, problem? Reseting";
		elseif eatTimer > 0 then
		  lastAte = "\nAte Last Grilled: " .. getElapsedTime(eatTimer);
		elseif buffed then
		  lastAte = "\nAte Last Grilled: Waiting on Buff to Expire";
		elseif not grilledOnion then
		  lastAte = "\nAte Last Grilled: Menu not pinned";
		else
		  lastAte = "\nAte Last Grilled: Never";
		end 

GUI = "Next Step: " .. step .. "/4 - " .. task_text .. "\n\n----------------------------------------------\n1) Straw Removed: " .. straw .."/" .. num_loops*per_rake .. "\n2) Tow Seperated: " .. tow .. "/" .. num_loops*per_rake .. "\n3) Lint Refined: " .. lint .. "/" .. num_loops*per_rake .. "\n4) Cleanings: " .. clean .. "/" .. num_loops .. "\n----------------------------------------------\n\nFlax Processed: " .. (loop_count-1)*per_rake .. "\nFlax Remaining: " .. (num_loops*per_rake) - straw .. "\nRepair Attempts: " .. repairAttempt .. "\n\nElapsed Time: " .. getElapsedTime(startTime) .. lastAte;



		if loop_count > num_loops or OK then
			num_loops = nil;

		elseif foundRepair then
		  repairRake();
		  clickAllText("This is"); -- Refresh window 
		  lsSleep(100);

		elseif not stats_black and not stats_black2 and not stats_black3 and not stats_blackB and not stats_blackC then
			sleepWithStatus(200, GUI, nil, 0.7, "Waiting on Endurance Timer");

		else
		
			clickAllText("This is");
			lsSleep(100);
			clickAllText(task);
			lsSleep(100);
			if step == 1 then
				straw = straw + per_rake;
			elseif step == 2 then
				tow = tow + per_rake;
			elseif step == 3 then
				lint = lint + per_rake;
			elseif
				step == 4 then
				clean = clean + 1;			
				step = 0;
				  if delay_loop_count then  -- We started macro while flax comb was in middle of processing... Finish this up before we advance loop counter (so that we can still process the "How much Flax?" at beginning.
				    delay_loop_count = nil;
				  else
				    loop_count= loop_count +1;
				  end
			end
			step = step + 1;
			sleepWithStatus(100, GUI, nil, 0.7, "Endurance OK - Clicking window(s)");
			clickAllText("This is")
			lsSleep(100);
		end

end

		lsPlaySound("Complete.wav");
		lsMessageBox("Elapsed Time:", getElapsedTime(startTime));
end


function checkCurrentStep()
  clickAllText("This is");
  lsSleep(100);
  taskStep1 = findText("Remove Straw");
  taskStep2 = findText("Separate Tow");
  taskStep3 = findText("Refine the Lint");
  taskStep4 = findText("Clean the");
  taskStep5 = findText("Repair");
  if taskStep1 then
    step = 1;
  elseif taskStep2 then
    step = 2;
  elseif taskStep3 then
    step = 3;
  elseif taskStep4 then
    step = 4;
  elseif taskStep5 then
    step = 1;
  else
    error("Could not find Flax Comb or Hackling Rake menus pinned");
  end
end


function repairRake()
  step = 1;
  lsPlaySound("error.wav");
  repairAttempt = repairAttempt + 1;
  sleepWithStatus(1000, "Attempting to Repair Rake !")
  local repair = findText("Repair")
  local material;
  local plusButtons;
  local maxButton;

  if repair then
    clickText(waitForText("Repair", 1000));
    clickText(waitForText("Load Materials", 1000));
    lsSleep(500);
    srReadScreen();
    plusButtons = findAllImages("plus.png");

	for i=1,#plusButtons do
		local x = plusButtons[i][0];
		local y = plusButtons[i][1];
             srClickMouseNoMove(x, y);

		if i == 1 then
		  material = "Boards";
		elseif i == 2 then
		  material = "Bricks";
		elseif i == 3 then
		  material = "Thorns";
		else
		  material = "What the heck?";
		end

             sleepWithStatus(1000,"Loading " .. material, nil, 0.7);

		srReadScreen();
		OK = srFindImage("ok.png")

		if OK then
		  sleepWithStatus(5000, "You don\'t have any \'" .. material .. "\', Aborting !\n\nClosing Build Menu and Popups ...", nil, 0.7)
		  srClickMouseNoMove(OK[0], OK[1]);
		  srReadScreen();
		  blackX = srFindImage("blackX.png");
		  srClickMouseNoMove(blackX[0], blackX[1]);
		  num_loops = nil;
		  break;

		else -- No OK button, Load Material

		  srReadScreen();
		  maxButton = srFindImage("max.png");
		  if maxButton then		
		    srClickMouseNoMove(maxButton[0], maxButton[1]);
		  end

		  sleepWithStatus(1000,"Loaded " .. material, nil, 0.7);
		end -- if OK
	end -- for loop
  end -- if repair
end
