function can_start_tutorial_time(time)
  return ((g_game:tutorial_manager():have_tutorials() ~= true) and (g_game:current_farm():time_left() > time))
end

local function bouquet_screen_tutorial_update_no_drag_item()
  local items_count = g_bouquets_screen:items_count()
  local bi = nil

  local i = 0

  if items_count > 0 then
    while (i < items_count) do
      bi = g_bouquets_screen:bi_by_index(i)
      if "p_fialka_1" == tostring(bi:inventory_item_name()) then
        i = items_count
      end
      i = i + 1
    end
--   
    if bi == nil then 
      bi = g_bouquets_screen:bi_by_index(0)
    end
--
    g_game:reset_tutorial_key("sell_one_bi_tutorial")
    g_game:start_tutorial("sell_one_bi_tutorial", bi:position())

    g_bouquets_screen:drag_bouquet_item_control():reset_drag_state_to_normal()
    g_bouquets_screen:drag_bouquet_item_control():set_need_bi(bi)

    while g_bouquets_screen:drag_bouquet_item_control():drag_item() == nil do
      coroutine.yield()
    end
	
	g_bouquets_screen:drag_bouquet_item_control():reset_need_bi();

    
    --    
    g_game:compleate_tutorial("sell_one_bi_tutorial")
    g_game:set_tutorial_key("sell_one_bi_tutorial")
    --

    g_game:reset_tutorial_key("sell_one_bi_tutorial_1")
    local pos = g_bouquets_screen:sell_one_bouquet_item_control():absolute_position()
    local dim = g_bouquets_screen:sell_one_bouquet_item_control():dimensions()
    g_game:start_tutorial("sell_one_bi_tutorial_1", Position(pos:x() + dim:width() / 2, pos:y() + dim:height() / 2))
    g_game:reset_tutorial_key("sell_one_bi_tutorial_1")
    --
  end
end

function bouquet_screen_tutorial() 
  print("start bouquet_screen_tutorial()")
  local items_count = g_bouquets_screen:items_count()
  
  if g_bouquets_screen == nil then
    return
  end

  if items_count < 0 then
    return
  end  
  
  local not_done = true
  local last_position = Position(0, 0)
  
  g_bouquets_screen:set_normal_drag_state();
  while not_done == true do
    if g_bouquets_screen:drag_bouquet_item_control():drag_item() == nil then
      g_bouquets_screen:sell_all_bouquet_item_control():hide()
      local inside = g_bouquets_screen:sell_one_bouquet_item_control():inside(last_position)
      
      if inside == true then
        not_done = false
        g_game:set_tutorial_key("sell_one_bi_tutorial_1")
        g_game:compleate_tutorial("sell_one_bi_tutorial_1")
        set_quest_status_by_path_action("location_1", "tutorial/bouquet_screen_tutorial", "quest_status_done", g_game) 
        g_game:current_town():mf_quests_manager():update()
        g_bouquets_screen:sell_all_bouquet_item_control():show(true)
        return
      end
      
      if inside ~= true then  
        g_game:compleate_tutorial("sell_one_bi_tutorial_1") 
        bouquet_screen_tutorial_update_no_drag_item()
      end
    end
   
    if g_bouquets_screen:drag_bouquet_item_control():drag_item() ~= nil then
      last_position = g_bouquets_screen:drag_bouquet_item_control():drag_item():position()
      g_bouquets_screen:sell_all_bouquet_item_control():show(false)
    end
    
    coroutine.yield()
  end
 --
   
   g_game:set_tutorial_key("sell_one_bi_tutorial_1")
   g_game:compleate_tutorial("sell_one_bi_tutorial_1")
   set_quest_status_by_path_action("location_1", "tutorial/bouquet_screen_tutorial", "quest_status_done", g_game) 
   g_bouquets_screen:sell_all_bouquet_item_control():show()
   
   g_game:current_town():mf_quests_manager():update()
end


local function bouquet_screen_tutorial_sell_all_fp() 
  local items_count = g_bouquets_screen:items_count()

  local t = {}
  local t1 = {}
  local i = 0
  
  if items_count > 0 then
    
	while (i < items_count) do
        local bi = g_bouquets_screen:bi_by_index(i)
	    i = i + 1
	
    	if t[bi:inventory_item_name()] == nil then
		   t[bi:inventory_item_name()] = 1
		   t1[bi:inventory_item_name()] = bi
		else
		   t[bi:inventory_item_name()] = t[bi:inventory_item_name()] + 1
		end
		
    end
	
	local val0
	local val1
	
	for val0, val1 in  pairs(t) do
	  if val1 > 1 then
		   return t1[val0]
	   end
	end
  end
  
  return nil 
end

function bouquet_screen_tutorial_sell_all() 
  print("start bouquet_screen_tutorial_all()")
  
  local not_done = true
  local bi = nil
  local last_position = Position(0, 0)
  
  g_bouquets_screen:set_normal_drag_state();
  
  while not_done == true do
    local inside = g_bouquets_screen:sell_all_bouquet_item_control():inside(last_position)

    if g_bouquets_screen:drag_bouquet_item_control():drag_item() == nil then

      if inside == true then
        g_game:set_tutorial_key("sell_all_bi_tutorial_1")
        g_game:compleate_tutorial("sell_all_bi_tutorial_1")
        set_quest_status_by_path_action("location_1", "tutorial/bouquet_screen_sell_flowers_tutorial", "quest_status_done", g_game) 
        g_game:current_town():mf_quests_manager():update()
        g_bouquets_screen:sell_one_bouquet_item_control():show(true)
        return
      else
        g_game:compleate_tutorial("sell_all_bi_tutorial_1")

        bi = bouquet_screen_tutorial_sell_all_fp() 
        
        if bi == nil then
          g_bouquets_screen:sell_one_bouquet_item_control():show(true)
          return
        end
        
        g_game:reset_tutorial_key("sell_all_bi_tutorial")
        g_game:start_tutorial("sell_all_bi_tutorial", bi:position())
        g_bouquets_screen:sell_one_bouquet_item_control():show(false)
        g_bouquets_screen:drag_bouquet_item_control():reset_drag_state_to_normal()    
  	    g_bouquets_screen:drag_bouquet_item_control():set_need_bi(bi)

        while g_bouquets_screen:drag_bouquet_item_control():drag_item() == nil do
          coroutine.yield()
        end
		
        g_bouquets_screen:drag_bouquet_item_control():reset_need_bi()
        
        g_game:compleate_tutorial("sell_all_bi_tutorial")
        g_game:set_tutorial_key("sell_all_bi_tutorial")

        g_game:reset_tutorial_key("sell_all_bi_tutorial_1")
        local pos = g_bouquets_screen:sell_all_bouquet_item_control():absolute_position()
        local dim = g_bouquets_screen:sell_all_bouquet_item_control():dimensions()
        g_game:start_tutorial("sell_all_bi_tutorial_1", Position(pos:x() + dim:width() / 2, pos:y() + dim:height() / 2))
        g_game:reset_tutorial_key("sell_all_bi_tutorial_1")
      end
    end
  
    if g_bouquets_screen:drag_bouquet_item_control():drag_item() ~= nil then
      last_position = g_bouquets_screen:drag_bouquet_item_control():drag_item():position()
      g_bouquets_screen:sell_one_bouquet_item_control():show(false)
    end

    coroutine.yield()
  end

  g_bouquets_screen:sell_all_bouquet_item_control():show(true)
end

local function can_gather_bouquet(bouquet)  
  local count_place = bouquet:n_places()
  local items_count = g_bouquets_screen:items_count()
   
  local t = 0

  tb = {}
  while (t < items_count) do
    local bi = g_bouquets_screen:bi_by_index(t)
    tb[t] = bi
    t = t + 1
  end
    
  local i = 0
  while (i < count_place) do
    local bi = bouquet:request(i)
    
    if bouquet:placed_item(i) == nil and bi:need_for_bouquet() == true then   
      local val0
      local val1
      local has_item = false
      
      for val0, val1 in pairs(tb) do
        if tb[val0] ~= nil and bi:equal(val1) then
          has_item = true
          tb[val0] = nil
          break
        end
      end
      
      if has_item == false then
        return false
      end
    end
    
    i = i + 1
  end
  
  return true
end

local function wait_to_get_bi(index)
 print ("wait_to_get_bi")
 local farm = g_game:current_farm()
 local bouquet = farm:bouquet(index)
 local founded = false
 local count_place = bouquet:n_places()
 local items_count = g_bouquets_screen:items_count()
 local place_n = -1

 local i = 0
 local bi = nil
 
 while (i < count_place) do
   bi = bouquet:request(i)
    
   if bouquet:placed_item(i) == nil and bi:need_for_bouquet() == true then
     founded = true
     place_n = i
     break
   end
   
   i = i + 1
 end

 if founded == true then
  local bi1 = nil
  local i = 0

  while (i < items_count) do
    if bi:equal(g_bouquets_screen:bi_by_index(i)) then
      bi1 = g_bouquets_screen:bi_by_index(i)
      break;
    end
    i = i + 1
  end

  g_bouquets_screen:drag_bouquet_item_control():reset_drag_state_to_normal()  
  g_bouquets_screen:drag_bouquet_item_control():set_need_bi(bi1)
  g_game:reset_tutorial_key("tutorial_sell_bouquet")
  g_game:start_tutorial("tutorial_sell_bouquet", bi1:position())
  
  while g_bouquets_screen:drag_bouquet_item_control():drag_item() == nil do
    coroutine.yield()
  end

  g_game:compleate_tutorial("tutorial_sell_bouquet") 
  g_bouquets_screen:drag_bouquet_item_control():reset_need_bi()
  return place_n
 end
end

function bouquet_screen_sell_bouquet()
  print_html("<FONT color=blue><b>Start tutorial:</FONT></b>bouquet_screen_sell_bouquet")
  local farm = g_game:current_farm()

  if farm:bouquet_targets_count() == 0 then
    return
  end
  
  local founded_bouquet = false
  local i = 0
  while (i < farm:bouquets_count()) do
    print(string.format("Can gather bouquet %i:", i + 1))
    print(can_gather_bouquet(farm:bouquet(i)))
    
    if (can_gather_bouquet(farm:bouquet(i))) then
      founded_bouquet = true
      break
    end
      
    i = i + 1
  end
  
  if founded_bouquet == false then
    return
  end
     
  local compleated = false
  
  local last_position = Position(0, 0)
  local global_pos = Position(1024, 768)
  
  local n_places = farm:bouquet(i):n_places()
  
  local i_p = 0
  g_bouquets_screen:set_normal_drag_state();
  while (i_p < n_places) do
    
    if (farm:bouquet(i):request(i_p):need_for_bouquet() == false) then
      n_places = n_places - 1
    end
  
    i_p = i_p + 1
  end
  

  g_bouquets_screen:drag_bouquet_item_control():reset_drag_state_to_normal()  
  g_bouquets_screen:drag_bouquet_item_control():set_need_bouquet_index(i)
  local current_placed = 0
  while compleated == false do
    if (g_bouquets_screen:drag_bouquet_item_control():drag_item() == nil) then
      g_game:compleate_tutorial("tutorial_sell_bouquet_1")
      
      local vec = Vector2I(global_pos, last_position)
      print (vec:length())

      if (vec:length() < 5) then
        print(string.format("n_places = %d, current_placed = %d", n_places, current_placed))
        
        if (n_places == current_placed + 1) then
        
          g_bouquets_screen:drag_bouquet_item_control():reset_need_bouquet_index()
          g_bouquets_screen:drag_bouquet_item_control():reset_bouquet_place_index()
          g_game:compleate_tutorial("tutorial_sell_bouquet_1")
          
          set_quest_status_by_path_action("location_1", "tutorial/bouquet_screen_sell_bouquet", "quest_status_done", g_game)
          g_game:current_town():mf_quests_manager():update()
          return
        end
      end

        local req = wait_to_get_bi(i)
        print (req)
        
        g_bouquets_screen:drag_bouquet_item_control():set_bouquet_place_index(req)
        
        last_position = g_bouquets_screen:drag_bouquet_item_control():drag_item():position()
        
        if (req ~= nil) then
          g_game:reset_tutorial_key("tutorial_sell_bouquet_1")
          g_game:start_tutorial("tutorial_sell_bouquet_1", farm:bouquet(i):global_place_position(req))
          global_pos = farm:bouquet(i):global_place_position(req)
        end

        current_placed = 0
        local i_p = 0
        
        while (i_p < farm:bouquet(i):n_places()) do
          if (farm:bouquet(i):request(i_p):need_for_bouquet() == true and farm:bouquet(i):placed_item(i_p) ~= nil) then
            current_placed = current_placed + 1
          end
  
          i_p = i_p + 1
        end
        
    end
    
    if g_bouquets_screen:drag_bouquet_item_control():drag_item() ~= nil then
      last_position = g_bouquets_screen:drag_bouquet_item_control():drag_item():position()
    end
    
    coroutine.yield()
  end
  
  g_bouquets_screen:drag_bouquet_item_control():reset_need_bouquet_index()
  g_bouquets_screen:drag_bouquet_item_control():reset_bouquet_place_index()
end

local function find_dropped_bug_item()
  local items_count = g_bouquets_screen:items_count()
  local bi = nil
  local founded = false

  local i = 0

  if items_count < 0 then
    return nil
  end
  
  while (i < items_count) do
    bi = g_bouquets_screen:bi_by_index(i)
    
    if (bi:need_for_bouquet() == false) then
      founded = true
      break
    end
    
    i = i + 1
  end
--
  if founded == false then
    return nil
  end

  g_bouquets_screen:drag_bouquet_item_control():reset_drag_state_to_normal()
  g_bouquets_screen:drag_bouquet_item_control():set_need_bi(bi)

  g_game:reset_tutorial_key("sell_dropped_item")
  g_game:start_tutorial("sell_dropped_item", bi:position())
  
  while g_bouquets_screen:drag_bouquet_item_control():drag_item() == nil do
    coroutine.yield()
  end

  g_game:compleate_tutorial("sell_dropped_item")
  g_game:set_tutorial_key("sell_dropped_item")
  g_bouquets_screen:drag_bouquet_item_control():reset_need_bi()
  
  return bi
end

function bouquet_screen_dropped_bug_item()
  print_html("<FONT color=blue><b>Start tutorial:</FONT></b>bouquet_screen_dropped_bug_item")

  local i = 0
  local j = 0

  local farm = g_game:current_farm()
  local compleated = false
  local has_bi = false

  
  for f_i = 0, farm:bouquets_count() - 1 do
    for f_j = 0, farm:bouquet(f_i):n_places() - 1 do

      print(string.format("f_i = %d, f_j = %d", f_i, f_j))

      if (farm:bouquet(f_i):request(f_j):need_for_bouquet() == false and farm:bouquet(f_i):placed_item(f_j) == nil) then
        has_bi = true
        i = f_i
        j = f_j
        break
      end
    end
    
    if (has_bi == true) then
      break
    end
  
  end
    
  
  if (has_bi == false) then
    return 
  end
  
  local last_position = Position(0, 0)
  local global_pos = farm:bouquet(i):global_place_position(j)

	g_bouquets_screen:set_normal_drag_state();  
  
  while (compleated == false) do
    if (g_bouquets_screen:drag_bouquet_item_control():drag_item() == nil) then
    
      local vec = Vector2I(global_pos, last_position)
      print (vec:length())

      if (vec:length() < 5) then
        break
      else
        g_game:compleate_tutorial("sell_dropped_item_1")
        local bi = find_dropped_bug_item()

        if (bi == nil) then
          g_bouquets_screen:drag_bouquet_item_control():reset_need_bouquet_index()
          g_bouquets_screen:drag_bouquet_item_control():reset_bouquet_place_index()
          return
        else
          print(string.format("i = %d, j = %d", i, j))
          g_bouquets_screen:drag_bouquet_item_control():reset_drag_state_to_normal()
          g_bouquets_screen:drag_bouquet_item_control():set_need_bouquet_index(i)
          g_bouquets_screen:drag_bouquet_item_control():set_bouquet_place_index(j)
          last_position = g_bouquets_screen:drag_bouquet_item_control():drag_item():position()
          
          g_game:reset_tutorial_key("sell_dropped_item_1")
          g_game:start_tutorial("sell_dropped_item_1", global_pos)
        end
        
      end   
    else
      last_position = g_bouquets_screen:drag_bouquet_item_control():drag_item():position()
    end
    
    coroutine.yield()
  end

  g_game:compleate_tutorial("sell_dropped_item_1")
  g_bouquets_screen:drag_bouquet_item_control():reset_need_bouquet_index()
  g_bouquets_screen:drag_bouquet_item_control():reset_bouquet_place_index()
  set_quest_status_by_path_action("location_1", "tutorial/bouquet_screen_dropped_bug_item", "quest_status_done", g_game) 
  g_game:current_town():mf_quests_manager():update()
end

function town_screen_tutorial()
  print_html("<FONT color=blue><b>Start tutorial:</FONT></b>town_screen_tutorial")
  if g_game == nil then
    return
  end
  
  while true do
      if in_screen("location_1", "town_screen", g_game) and (g_town_screen ~= nil) and (g_town_screen:large_quest_holder() == true) and (g_town_screen:large_quest_full_showed()) then
	break
      end
      
      if (in_screen("location_1", "town_screen", g_game) == false) then
        return
      end 

      coroutine.yield()
  end  

  if (g_town_screen ~= nil) and (g_town_screen:large_quest_holder() == true) and (g_town_screen:large_quest_full_showed()) then
    g_game:reset_tutorial_key("town_screen_first_talk")
    local pos = Position()
    pos:set_x((g_town_screen:large_quest_holder_position():x() + g_town_screen:large_quest_holder_dimensions():width()) / 2)
    pos:set_y((g_town_screen:large_quest_holder_position():y()))
    g_game:start_tutorial("town_screen_first_talk", pos)
    set_quest_status_by_path_action("location_1", "tutorial/town_screen_tutorial", "quest_status_done", g_game) 
  end
end

function sell_veg_tutorial()
  print_html("<FONT color=blue><b>Start tutorial:</FONT></b>sell_veg_tutorial")
  
  if (g_shop_screen_farm == nil) then
    return
  end

  if (g_shop_screen_farm:shop_control() == nil) then
    return
  end
  
  local rsi = g_shop_screen_farm:shop_control():ridges_shop_item();
  
  if (rsi == nil) then
    return
  end
  
  if (rsi:have_sell_button() == true) and (rsi:sell_button_visible() == true) then
    g_game:reset_tutorial_key("sell_veg_tutorial")
    g_game:start_tutorial("sell_veg_tutorial", rsi:sell_button_center_position())
    
    set_quest_status_by_path_action("location_1", "tutorial/sell_veg_tutor", "quest_status_done", g_game) 
  end
end