class "malahit_vegetation_function" (VegetationFunction)

function create_malahit_vegetation_function()
	return malahit_vegetation_function()
end

function malahit_vegetation_function:__init() super()
end

function malahit_vegetation_function:init()
  self.clicks_count = 50
  self.current_click_count = 0
  self.delay_next_activation = 10
  self.delay_next_deactivation = 5
  self.next_activation = 0
  self.next_producing_time = 15
  self.next_producing_time_current = self.next_producing_time
  self.actived = false
  self.completed = false
  self:register_renderable("active", false);
  self:register_renderable("final", false);
end

function malahit_vegetation_function:update(dt)
  if (self.completed == false) then
    self.next_activation = self.next_activation + dt
 
    if ((self.next_activation >= self.delay_next_activation) and self.actived == false) then
      self.actived = not self.actived
      self.next_activation = 0   
    end       

    if ((self.next_activation >= self.delay_next_deactivation) and self.actived == true) then
      self.actived = not self.actived
      self.next_activation = 0   
    end       

    if (self.actived) then
       self:vegetation():set_force_brake_by_state("active", false)
       self:vegetation():set_state("active")
       self:vegetation():show_custom_progress();
    else
       self:vegetation():set_force_brake_by_state("active", true)
       self:vegetation():set_state("normal")
       self:vegetation():hide_custom_progress();
    end

    self:vegetation():set_custom_progress(self.current_click_count / self.clicks_count);

    if (self.current_click_count >= self.clicks_count) then
      self.completed = true
      self:vegetation():set_force_brake_by_state("active", true)
      self:vegetation():set_state("final")  
      self:vegetation():set_force_brake_by_state("final", false)
      self.next_producing_time_current = self.next_producing_time
    end
  else
    self:vegetation():hide_custom_progress();    

    if (self.next_producing_time_current > 0) then
      self.next_producing_time_current = self.next_producing_time_current - dt;
    end

    if (self.next_producing_time_current < 0) and (have_marks("malahit_flower", g_game) ~= true) then
      self.next_producing_time_current = 0
      self:vegetation():add_product("p_malahit_3", 0);
    end

  end
end

function malahit_vegetation_function:save(ios)
  ios:save(4)
  ios:save(self.clicks_count)
  ios:save(self.current_click_count)
  ios:save(self.next_activation)
  ios:save(self.actived)
  ios:save(self.completed)  
  ios:save(self.next_producing_time_current)
end

function malahit_vegetation_function:load(ios)
  local ver = ios:load()

  if (ver >= 1) then
    self.clicks_count = ios:load()
    self.current_click_count = ios:load()
  end

  if (ver >= 2) then
    self.next_activation = ios:load()
    self.actived = ios:load()
  end

  if (ver >= 3) then
    self.completed = ios:load() 
  end

  if (ver >= 4) then
    self.next_producing_time_current = ios:load()
  end
end

function malahit_vegetation_function:cursor(p)
  self.rectangle = Rectangle(self:vegetation():position(), Dimensions(120, 200))

  if (self.completed) then
    return ""
  end

  if (self.rectangle:contains(p)) and (self.actived) then
    return "kirka"
  end

  return ""
end

function malahit_vegetation_function:on_click(p)
  self.rectangle = Rectangle(self:vegetation():position(), Dimensions(120, 200))

  if (self.completed) then
    return false
  end


  if (self.rectangle:contains(p)) and (self.actived) then
    self.current_click_count = self.current_click_count + 1
    print(self.current_click_count)
    return true
  end

  return false
end

function malahit_vegetation_function:restart()
  self.next_producing_time_current = self.next_producing_time

  if (self.completed == false) then  
    self:vegetation():set_force_brake_by_state("active", true)
    self:vegetation():set_state("normal")
    self:vegetation():hide_custom_progress();

    self.actived = false
    self.next_activation = 0   
  end

end
