-- не настраивать


function getNameProduct(aObject,aSizeTableName)
    
    if (not aObject) then return ""; end
    if (aSizeTableName < aObject.Statics.indexStatics or aObject.Statics.indexStatics < 1) then return ""; end
    local aname = "";
    local beg = aObject.Statics.indexStatics;
    for ind = beg, aSizeTableName, 1 do
        aname = aObject.Statics.ProductName[ind];
        --ScriptDump(""..aObject.Statics.indexStatics);
        if (aObject.Statics[aname] ~= nil) then
            if (aObject.Statics[aname] >= 0) then
                aObject.Statics.indexStatics = ind + 1;
                return aname;
            end
        end
    end
    aObject.Statics.indexStatics = aSizeTableName + 1;
    return "";
end

-- если aLevelEnd = 0 то это вызов перед уровнем
function setDefLevelInfo(aObj,aLevelEnd)
    local money = 0;
    --aObj.Statics.shear = 30;
    --aObj.Statics.milk = 10;
    --aObj.Statics.score = 5000;
    end_level_info = {};
	
    local sizeStatics = 0;
    aObj.Statics.indexStatics = 1;
    if (aObj ~= nil and aObj.Statics.ProductName ~= nil) then
        sizeStatics = table.getn(aObj.Statics.ProductName);
    end

    local valueProd = -1;
    local textProd = "empty_text";
    local nameProd = "";
    local moneyProd = 0;
    -- 1 line
    if (aObj ~= nil and aObj.Statics.ProductName ~= nil) then 
        nameProd = getNameProduct(aObj,sizeStatics); --aObj.Statics,aObj.Statics.ProductName,sizeStatics);
    end
    if (nameProd ~= "") then
        valueProd = aObj.Statics[nameProd];
        textProd = "text_"..nameProd;
    else
        valueProd = -1;
        textProd = "empty_text";
    end
    end_level_info.description_line1 = {};
    end_level_info.description_line1.value = textProd;
    end_level_info.value_line1 = {};
    if (aObj ~= nil) then 
        if (valueProd >= 0) then
            moneyProd = aObj.Money[nameProd];
            end_level_info.value_line1.value = ""..valueProd;
            end_level_info.value_line1.score = ""..moneyProd;
            end_level_info.value_line1.name = nameProd;
            money = money + moneyProd;
        else
            end_level_info.value_line1.name = "";
            end_level_info.value_line1.value = "-1";
            end_level_info.value_line1.score = "0";
        end
    else
        end_level_info.value_line1.value = "";
        end_level_info.value_line1.score = "0";
    end
    -- 2 line
    if (aObj ~= nil and aObj.Statics.ProductName ~= nil) then 
        nameProd = getNameProduct(aObj,sizeStatics); --aObj.Statics,aObj.Statics.ProductName,sizeStatics);
    end
    if (nameProd ~= "") then
        valueProd = aObj.Statics[nameProd];
        textProd = "text_"..nameProd;
    else
        valueProd = -1;
        textProd = "empty_text";
    end
    end_level_info.description_line2 = {};
    end_level_info.description_line2.value = textProd;
    end_level_info.value_line2 = {};
    if (aObj ~= nil) then 
        if (valueProd >= 0) then
            moneyProd = aObj.Money[nameProd];
            end_level_info.value_line2.value = ""..valueProd;
            end_level_info.value_line2.score = ""..moneyProd;
            end_level_info.value_line2.name = nameProd;
            money = money + moneyProd;
        else
            end_level_info.value_line2.value = "-1";
            end_level_info.value_line2.score = "0";
            end_level_info.value_line2.name = "";
        end
    else
        end_level_info.value_line2.value = "";
        end_level_info.value_line2.score = "0";
    end
    -- 3 line
    if (aObj ~= nil and aObj.Statics.ProductName ~= nil) then 
        nameProd = getNameProduct(aObj,sizeStatics); --aObj.Statics,aObj.Statics.ProductName,sizeStatics);
    end
    if (nameProd ~= "") then
        valueProd = aObj.Statics[nameProd];
        textProd = "text_"..nameProd;
    else
        valueProd = -1;
        textProd = "empty_text";
    end
    end_level_info.description_line3 = {};
    end_level_info.description_line3.value = textProd;
    end_level_info.value_line3 = {};
    if (aObj ~= nil) then 
        if (valueProd >= 0) then
            moneyProd = aObj.Money[nameProd];
            end_level_info.value_line3.value = ""..valueProd;
            end_level_info.value_line3.score = ""..moneyProd;
            end_level_info.value_line3.name = nameProd;
            money = money + moneyProd;
        else
            end_level_info.value_line3.name = "";
            end_level_info.value_line3.value = "-1";
            end_level_info.value_line3.score = "0";
        end
    else
        end_level_info.value_line3.value = "";
        end_level_info.value_line3.score = "0";
    end
    -- 4 line
    if (aObj ~= nil and aObj.Statics.ProductName ~= nil) then 
        nameProd = getNameProduct(aObj,sizeStatics); --aObj.Statics,aObj.Statics.ProductName,sizeStatics);
    end
    if (nameProd ~= "") then
        valueProd = aObj.Statics[nameProd];
        textProd = "text_"..nameProd;
    else
        valueProd = -1;
        textProd = "empty_text";
    end
    end_level_info.description_line4 = {};
    end_level_info.description_line4.value = textProd;
    end_level_info.value_line4 = {};
    if (aObj ~= nil) then 
        if (valueProd >= 0) then
            moneyProd = aObj.Money[nameProd];
            end_level_info.value_line4.value = ""..valueProd;
            end_level_info.value_line4.score = ""..moneyProd;
            end_level_info.value_line4.name = nameProd;
            money = money + moneyProd;
        else
            end_level_info.value_line4.name = "";
            end_level_info.value_line4.value = "-1";
            end_level_info.value_line4.score = "0";
        end
    else
        end_level_info.value_line4.value = "";
        end_level_info.value_line4.score = "0";
    end
    -- 5 line
    if (aObj ~= nil and aObj.Statics.ProductName ~= nil) then 
        nameProd = getNameProduct(aObj,sizeStatics); --aObj.Statics,aObj.Statics.ProductName,sizeStatics);
    end
    if (nameProd ~= "") then
        valueProd = aObj.Statics[nameProd];
        textProd = "text_"..nameProd;
    else
        valueProd = -1;
        textProd = "empty_text";
    end
    end_level_info.description_line5 = {};
    end_level_info.description_line5.value = textProd;
    end_level_info.value_line5 = {};
    if (aObj ~= nil) then 
        if (valueProd >= 0) then
            moneyProd = aObj.Money[nameProd];
            end_level_info.value_line5.value = ""..valueProd;
            end_level_info.value_line5.score = ""..moneyProd;
            end_level_info.value_line5.name = nameProd;
            money = money + moneyProd;
        else
            end_level_info.value_line5.name = "";
            end_level_info.value_line5.value = "-1";
            end_level_info.value_line5.score = "0";
        end
    else
        end_level_info.value_line5.value = "";
        end_level_info.value_line5.score = "0";
    end
    -- 6 line
    if (aObj ~= nil and aObj.Statics.ProductName ~= nil) then 
        nameProd = getNameProduct(aObj,sizeStatics); --aObj.Statics,aObj.Statics.ProductName,sizeStatics);
    end
    if (nameProd ~= "") then
        valueProd = aObj.Statics[nameProd];
        textProd = "text_"..nameProd;
    else
        valueProd = -1;
        textProd = "empty_text";
    end
    end_level_info.description_line6 = {};
    end_level_info.description_line6.value = textProd;
    end_level_info.value_line6 = {};
    if (aObj ~= nil) then 
        if (valueProd >= 0) then
            moneyProd = aObj.Money[nameProd];
            end_level_info.value_line6.value = ""..valueProd;
            end_level_info.value_line6.score = ""..moneyProd;
            end_level_info.value_line6.name = nameProd;
            money = money + moneyProd;
        else
            end_level_info.value_line6.name = "";
            end_level_info.value_line6.value = "-1";
            end_level_info.value_line6.score = "0";
        end
    else
        end_level_info.value_line6.value = "";
        end_level_info.value_line6.score = "0";
    end
    
--bonus score    
    end_level_info.description_line7 = {};
    end_level_info.description_line7.value = "bonus_score_text";
    end_level_info.value_line7 = {};
    if (aObj ~= nil) then
        end_level_info.value_line7.value = ""..aObj.Statics.bonus_score;
    else
        end_level_info.value_line7.value = "";
    end
    --разнести бонусы по типам
    end_level_info.bonuses_score = {};
    end_level_info.bonuses_score.line1 = {};
    local chaining_bonus = aObj.Statics.bonus_score - (bonuses.Car + bonuses.Telephon);
    end_level_info.bonuses_score.line1.value = ""..chaining_bonus;
    end_level_info.bonuses_score.line1.description = "chain_result";
    
    end_level_info.bonuses_score.line2 = {};
    end_level_info.bonuses_score.line2.value = ""..bonuses.Car;
    end_level_info.bonuses_score.line2.description = "car_result";
    
    end_level_info.bonuses_score.line3 = {};
    end_level_info.bonuses_score.line3.value = ""..bonuses.Telephon;
    end_level_info.bonuses_score.line3.description = "telephon_result";
    
    --end_level_info.total_money = {};
    --if (aObj ~= nil) then
    --    end_level_info.total_money.value = ""..money;
    --else
    --    end_level_info.total_money.value = "";
    --end
-- LEVEL score    
    end_level_info.description_line8 = {};
    end_level_info.description_line8.value = "level_score_text";
    end_level_info.value_line8 = {};
    if (aObj ~= nil) then
        end_level_info.value_line8.value = ""..aObj.Statics.score;
    else
        end_level_info.value_line8.value = "";
    end

    end_level_info.level_text = {};
    end_level_info.level_result = {};
    if (aLevelEnd == 1) then
        local lev_res = isLevelWin();
        if (lev_res == 0) then  --мы проиграли
            end_level_info.level_text.value = "result_description_lose";
            end_level_info.level_result.value = "0";
            SetStatParamI("EndState", 1);            
            end_level_info.level_result.expert = "0";
        else
            end_level_info.level_text.value = "result_description_win";
            end_level_info.level_result.value = "1";           
            SetStatParamI("EndState", 0);
            end_level_info.level_result.expert = "0";
            if (aObj ~= nil) then
                if (aObj.Statics.score >= aObj.Params.ExpertScoresLimit) then
                    end_level_info.level_result.expert = "1";
                    SetStatParamI("ExpertAchieved", 1);
                end
            end
        end
    else
    -- не определяйте end_level_info.level_result при aLevelEnd == 0
        end_level_info.level_text.value = "";
    end
    
    end_level_info.back_image = {};
    
    if (aLevelEnd == 1) then
        end_level_info.back_image.image="backgrounds/Level_results3_2";
    else
        end_level_info.back_image.image="backgrounds/Level_results3_1";
    end
    end_level_info.next_level_text = {};
    end_level_info.next_level_text.value = "level_target";
end
