shadow_widget = shadow_widget or {}

__inherite(shadow_widget, null_lua_widget_handler())

shadow_widget.visible_flag = nil
--[[

shadow_widget.current_color = sf.graphics.Color(0, 0, 0, 0)

function shadow_widget.Create(_parent_widget) --_this, _renderer
        shadow_widget.shadow_color = string_to_color(quest.game_resources.GetChild(_T("Constants"), false).GetChild(_T("shadow_widget"), false).GetValue(_T("shadow_color")).c_str())

        local new_lua_widget = sf.gui.CLuaWidget("shadow_widget", 1, 0)
        shadow_widget.self = new_lua_widget
        new_lua_widget.SetLua("qe_level")
        new_lua_widget.SetOverloadTable("shadow_widget")
        local shadow_widget_size = sf.misc.FloatVector(_parent_widget.GetSize())
        local size_x = tonumber(quest.game_resources.GetChild(_T("Constants"), false).GetChild(_T("shadow_widget"), false).GetValue(_T("size_x")).c_str())
        local size_y = tonumber(quest.game_resources.GetChild(_T("Constants"), false).GetChild(_T("shadow_widget"), false).GetValue(_T("size_y")).c_str())
        if size_x then
            shadow_widget_size.X = size_x
        end        
        if size_y then
            shadow_widget_size.Y = size_y
        end        
        new_lua_widget.SetSize(shadow_widget_size.X, shadow_widget_size.Y)
        local parent_offset = _parent_widget.GetOffset()
        new_lua_widget.SetOffset(-parent_offset.X, -parent_offset.Y)
        _parent_widget.AddWidget(shadow_widget.self)
        shadow_widget.Show(false)
end
]]

--[[

function shadow_widget.DoDraw(_this, _renderer)
    --local self_size = shadow_widget.self.GetSize()
    _renderer.RenderFilledPoly4(sf.misc.Poly4FromRect(sf.misc.FloatRect(0, 0, shadow_widget.size.X, shadow_widget.size.Y)), shadow_widget.current_color);
	return false
end
function shadow_widget.DoUpdate(_this)
	return false
end
]]
function shadow_widget.__CreateInstance()
    return "shadow_widget"
end
function shadow_widget.Show(_value)
    if shadow_widget.visible_flag ~= _value then
        assert(shadow_widget.self)
        if _value ~= nil then
            shadow_widget.visible_flag = _value
            set_widget_flag_value(shadow_widget.self, sf.gui.CBaseWidget.FlagDisabled, not _value)  
            set_widget_flag_value(shadow_widget.self, sf.gui.CBaseWidget.FlagHidden, not _value)     
        end
    end
end

function shadow_widget.OnMouseMove(_this, _pos, _state, _broadcast)
    if shadow_widget.visible_flag then
	return true
	end
	return false
end



function shadow_widget.Load(_this, _info, _templates)
        --shadow_widget.shadow_color = string_to_color(quest.game_resources.GetChild(_T("Constants"), false).GetChild(_T("shadow_widget"), false).GetValue(_T("shadow_color")).c_str())

        --local new_lua_widget = sf.gui.CLuaWidget("shadow_widget", 1, 0)
        --shadow_widget.self = new_lua_widget
        shadow_widget.self = _this
        shadow_widget.size = _this.GetSize()
        --new_lua_widget.SetLua("qe_level")
        --new_lua_widget.SetOverloadTable("shadow_widget")
        --local shadow_widget_size = sf.misc.FloatVector(_parent_widget.GetSize())
        --local size_x = tonumber(quest.game_resources.GetChild(_T("Constants"), false).GetChild(_T("shadow_widget"), false).GetValue(_T("size_x")).c_str())
        --local size_y = tonumber(quest.game_resources.GetChild(_T("Constants"), false).GetChild(_T("shadow_widget"), false).GetValue(_T("size_y")).c_str())
        --if size_x then
          --  shadow_widget_size.X = size_x
        --end        
        --if size_y then
          --  shadow_widget_size.Y = size_y
        --end        
        --new_lua_widget.SetSize(shadow_widget_size.X, shadow_widget_size.Y)
        --local parent_offset = _parent_widget.GetOffset()
        --new_lua_widget.SetOffset(-parent_offset.X, -parent_offset.Y)
        --_parent_widget.AddWidget(shadow_widget.self)
        shadow_widget.Show(false)

--[[
    if shadow_widget.visible_flag then
	return true
	end
	return false
]]
end



shadow_widget._effect_timer = sf.core.CTimerWrapper(sf.core.g_Application.GetTimeManager().GetTimer(_T("qe_level")).AttachTimer(_T("")))

--[[
shadow_widget = shadow_widget or {}


__inherite(shadow_widget, null_lua_widget_handler())

function shadow_widget.__CreateInstance()
    return "shadow_widget"
end

]]

shadow_widget.go_dark = nil  
shadow_widget.go_dark_time = 2000
shadow_widget.go_light_time = 2000
shadow_widget.effect_time = nil
shadow_widget.effect_start = nil

shadow_widget.max_alpha = 200

shadow_widget.dark_color = sf.graphics.Color(0, 0, 0, 0)

function shadow_widget.StartLight(_time)

    shadow_widget.go_dark = 1
    shadow_widget.effect_start = true
    shadow_widget.dark_color = sf.graphics.Color(shadow_widget.max_alpha, 0, 0, 0)
    if _time then
        shadow_widget.effect_time = _time
    else
        shadow_widget.effect_time = shadow_widget.go_light_time
    end

    shadow_widget.Show(true)

end

function shadow_widget.StartDark(_time)
    shadow_widget.go_dark = 2
    shadow_widget.effect_start = true
    shadow_widget.dark_color = sf.graphics.Color(0, 0, 0, 0)
    if _time then
        shadow_widget.effect_time = _time
    else
        shadow_widget.effect_time = shadow_widget.go_dark_time
    end
    shadow_widget.Show(true)

end

function shadow_widget.DoUpdate(_this)
    if shadow_widget.go_dark then
        if shadow_widget.effect_start then
            shadow_widget.effect_start = nil
            shadow_widget._effect_timer.Get().SetTime(0)    
        end
    
        local time = shadow_widget._effect_timer.Get().GetTime()
        local koef = time / shadow_widget.effect_time
        local finish_effect = false
        if time > shadow_widget.effect_time then
            koef = 1
            finish_effect = true
        end

        if shadow_widget.go_dark == 1 then

            koef = 1 - koef
        end
        
        shadow_widget.dark_color = sf.graphics.Color((koef) * shadow_widget.max_alpha, 0, 0, 0)
        

        if finish_effect then
            if shadow_widget.go_dark == 1 then
                shadow_widget.Show(false)

            end
            shadow_widget.go_dark = nil
            shadow_widget.change_music_volume = nil
        end

    end
    return true
end

function shadow_widget.DoDraw(_this, _renderer)
    _renderer.RenderFilledPoly4(sf.misc.Poly4FromRect(sf.misc.FloatRect(0, 0, shadow_widget.size.X, shadow_widget.size.Y)), shadow_widget.dark_color)
    return true
end







