----------------------------------------------------------------------------------------------------
-- mob_trader
----------------------------------------------------------------------------------------------------
global_animations = {
           desk_idle                                = {"idle","idle_1","palci","zatylok"},
    desk_talk                                = {"idle"},
        desk_to_chair_hack                = {"idle_spinka"},
        desk_to_listen                        = {"idle_chitaet"},
        desk_to_diagonal                = {"perehod_0"},

        chair_hack_idle                        = {"idle_2","idle_2_def_0"},
        chair_hack_talk                        = {"idle_2_talk_0","idle_2_talk_1","idle_2_talk_2","idle_2_talk_3"},
        chair_hack_to_diagonal        = {"idle_2_to_idle_0"},
        chair_hack_to_desk                = {"spinka_idle"},

        compute_idle                                = {"idle_1_spec_think_0","idle_1_spec_think_1"},
        compute_idle_to_diagonal        = {"idle_1_to_idle_0"},

        diagonal_idle                        = {"idle_0_def_0","idle_0_spec_click_0","idle_0_spec_scratch_0"},
        diagonal_idle_short                = {"idle_0_def_0"},
        diagonal_talk                        = {"idle_0_talk_0","idle_0_talk_1","idle_0_talk_2","idle_0_talk_3"},
        diagonal_to_chair_hack        = {"idle_0_to_idle_2"},
        diagonal_to_compute                = {"idle_0_to_idle_1"},
        diagonal_to_desk                = {"perehod_1"},

        first_phrase                        = {"vstuplenie"},

        listen_idle                                = {"chitaet"},
        listen_to_desk                        = {"chitaet_idle"},

        hello                                = {"hello"},

        forester_sit                 = {"sit"},
        forester_beard                = {"beard_in","beard","beard_out"},
        forester_reaction        = {"reaction_in","reaction","reaction_out"},
        forester_see                = {"see_in","see","see_out"},
        forester_talk_1                = {"sit_talk_1_in","sit_talk_1","sit_talk_1_out"},
        forester_talk_2                = {"sit_talk_2_in","sit_talk_2","sit_talk_2_out"},
        forester_talk_3                = {"sit_talk_3_in","sit_talk_3","sit_talk_3_out"}


}

head_animations = {
		forester_talk_1    		= {"sit_talk_1_in","sit_talk_1","sit_talk_1_out"},
		forester_talk_2    = {"sit_talk_2_in","sit_talk_2","sit_talk_2_out"},
		forester_talk_3    = {"sit_talk_3_in","sit_talk_3","sit_talk_3_out"},
		forester_reaction        = {"reaction_in","reaction","reaction_out"},
        normal             = {"talk_0","talk_1","talk_4"},
        angry              = {"talk_3","talk_2","talk_1"},
        good               = {"talk_glad_0"},
        first_phrase       = {"vstuplenie_head"}
}

----------------------------------------------------------------------------------------------------
function on_start_tutorial_item_1()
        if db.trader == nil then return end

        db.trader:external_sound_start("characters_voice\\scenario\\trader\\trader_tutorial_pda_12")
end

function on_stop_tutorial_item()
        if db.trader == nil then return end
        db.trader:external_sound_stop()
end
----------------------------------------------------------------------------------------------------

class "mob_trader"

function mob_trader:__init(obj, storage)
        self.object = obj
        self.st                = storage
end

function mob_trader:reset_scheme()
        trader_obj = self.object

        self.st.signals = {}

        self.object:set_callback(callback.trader_global_anim_request, self.on_global_anim_request, self )
        self.object:set_callback(callback.trader_head_anim_request, self.on_head_anim_request, self )
        self.object:set_callback(callback.trader_sound_end, self.on_sound_end, self )
        self.object:set_callback(callback.use_object, self.use_callback, self)

        -- start global animation
        self.object:set_trader_global_anim(self:select_global_animation())

        -- start sound and head animation
        if (self.st.sound_phrase) then
                local snd = mob_sound.pick_sound_from_set(self.object, self.st.sound_phrase, {})
                self.object:set_trader_sound(snd, self:select_head_animation());
        end

        if self.st.anim_head == nil then
                self.st.anim_head = "normal"
        end

        db.trader = self.object
end

function mob_trader:use_callback()
--    dialog_manager.restore_npc_disabled_phrases(self.object:id())
end

function mob_trader:update(delta)
        if xr_logic.try_switch_to_another_section(self.object, self.st, db.actor) then
                return
        end
end

---------------------------------------------------------------------------------------------------------------------
function mob_trader:select_global_animation()
        local anim_set = global_animations[self.st.anim_global]
    local r = math.random(1, #anim_set)
        return anim_set[r]
end

function mob_trader:select_head_animation()
        -- random select
        local anim_set = head_animations[self.st.anim_head]
    local r = math.random(1, #anim_set)
        return anim_set[r]
end

---------------------------------------------------------------------------------------------------------------------

function mob_trader:on_global_anim_request()
        self.st.signals["animation_end"] = true

        if (not self.st.play_once) then
                self.object:set_trader_global_anim(self:select_global_animation())
        end
end

function mob_trader:on_head_anim_request()
        if self.st.anim_head == nil then return end

        self.object:set_trader_head_anim(self:select_head_animation());
end

function mob_trader:on_sound_end()
        self.st.signals["sound_phrase_end"] = true
end
---------------------------------------------------------------------------------------------------------------------
function mob_trader:deactivate()
        self.object:set_callback(callback.trader_global_anim_request, nil )
        self.object:set_callback(callback.trader_head_anim_request, nil )
        self.object:set_callback(callback.trader_sound_end, nil )
        self.object:set_callback(callback.use_object, nil )
        self.st.signals = {}
        db.trader                = nil
end

---------------------------------------------------------------------------------------------------------------------
function add_to_binder(npc, ini, scheme, section, storage)
        printf("DEBUG: add_to_binder: npc:name()='%s', scheme='%s', section='%s'", npc:name(), scheme, section)

        local new_action = mob_trader(npc, storage)

        -- Зарегистрировать все actions, в которых должен быть вызван метод reset_scheme при изменении настроек схемы:
        xr_logic.subscribe_action_for_events(npc, storage, new_action)
end

function set_scheme(npc, ini, scheme, section, gulag_name)
        local st        = xr_logic.assign_storage_and_bind(npc, ini, scheme, section)
        st.logic        = xr_logic.cfg_get_switch_conditions(ini, section, npc)

        st.anim_global    = utils.cfg_get_string(ini, section, "anim_global", npc, true,  "")
        st.can_talk                  = utils.cfg_get_bool(ini,   section, "can_talk",    npc, false, true)
        st.tip_text                  = utils.cfg_get_string(ini, section, "tip_text",    npc, false, "", "character_use")
        if st.tip_text == nil then
                st.tip_text = ""
        end
        npc:set_tip_text(st.tip_text)

        if (ini:line_exist( section, "sound_phrase")) then
                st.anim_head        = utils.cfg_get_string(ini, section, "anim_head", npc, false, "")
                st.sound_phrase        = utils.cfg_get_string(ini, section, "sound_phrase", npc, false, "")
        else
                st.anim_head        = nil
                st.sound_phrase        = nil
        end


        --' check if play once animation
        st.play_once = false
        if (ini:line_exist( section, "on_signal")) then
                local str = utils.cfg_get_string(ini, section, "on_signal", npc, false, "")
                local par = utils.parse_params(str)
                if par[1] == "animation_end" then
                        st.play_once = true
                end
        end

        if st.can_talk == true then
                npc:enable_talk()
        else
                npc:disable_talk()
        end

end
