import QtQuick 1.1
import QtWidgetProxies 1.0
import "../common/widgets" as Widgets
import "../common/Common.js" as Common
import "../common/fonts" as Fonts
import "../common/EnumHelper.js" as EnumHelper

Item
{
	id: dashboard
	objectName: "Dashboard";
	signal problemListButtonPressed
	property int tabOrder: 1

	property variant fontStyles: Fonts.Fonts{}
	property variant context: MainWindow.dashboard
	
	Localizer { id:tr; scope:"Dashboard" }
	
	function getSubscriptionRemainingDays()
	{
        if(context.ShouldShowRemainingSubscriptionDays)
        {
            if(context.ActiveLicenseRemainingDays != 0)
               return tr.Localize("SubscriptionActive", {"LicenseState_RemainingDaysCount": context.LicenseRemainingDaysCount});
            else
               return tr.Localize("ExpiredToday");
        }
		return tr.Localize("SubscriptionActiveExpirationDateUnknown");
	}
	
	function getLicenseExpiredEx()
	{
		if(context.ActiveLicenseRemainingDays != 0)
			return tr.Localize("LicenseRemainDays", {"LicenseState_RemainingDaysCount": context.LicenseRemainingDaysCount});
		else
			return tr.Localize("ExpiredToday");
	}
	
	function getButtonTypeName()
	{
		var customTitle = context.BuyRenewCustomTitle;
		if(customTitle != "")
			return customTitle;
		if(EnumHelper.getKey(context, "BuyRenewType") == "Buy")
			return tr.Localize("BuyNow");
		if(EnumHelper.getKey(context, "BuyRenewType") == "Renew")
			return tr.Localize("Renew");
		return "";
	}
	
	function getRecommendationsStateDescription()
	{
		if(EnumHelper.getKey(context, "RecomendationStatus") == "EnableAutomaticUpdates")
			return tr.Localize("EnableAutomaticUpdates");
		if(EnumHelper.getKey(context, "RecomendationStatus") == "TrainAntiSpam")
			return tr.Localize("TrainAntiSpam");
		return "";
	}

	Widgets.BaseButton
	{
	    id: dashboardButton
	    objectName: "DashboardButton";
	    anchors.fill: parent
	    onClicked: dashboard.problemListButtonPressed()
        focusRectEnabled: false

        Widgets.FocusImage
        {
            anchors {
                verticalCenter: statusImage.verticalCenter
                verticalCenterOffset: -5
                horizontalCenter: statusImage.horizontalCenter
            }
            source: "../common/images/focuses/focus_product_status.png"
        }

        Image
	    {
	        id: image;
            anchors.fill: parent
            source: dashboardButton.selectValue("", "../common/images/main_window/contentarea_mainscreen.png",
                "../common/images/main_window/contentarea_mainscreen_hover.png",
	            "../common/images/main_window/contentarea_mainscreen_pressed.png")
        }

	    function getProductStateImage(object, propertyName)
	    {
	        return EnumHelper.convert(object, propertyName, {
	                        "Unknown":"",
	                        "Info":"../common/images/main_window/computer_ok.png",
	                        "Warning":"../common/images/main_window/computer_problem.png",
                            "Critical":"../common/images/main_window/computer_risk.png"});
	    }

	    Image
	    {
	        id: statusImage
            anchors.left:parent.left
            anchors.leftMargin: 53
            anchors.top: statusesWrapper.top
            Widgets.DPIScaler { id: dpiScaler }
	        Widgets.DPIScaler{changeSize:true; changeX:true}
            source: dashboardButton.getProductStateImage(context, "ProductState")
            smooth: true
	    }        

        Item
        {
            id: statusesWrapper
            height: childrenRect.height
            width: childrenRect.width
            anchors.left : statusImage.right
            anchors.leftMargin : 32
            anchors.right: parent.right
            anchors.rightMargin: 30
            anchors.top: parent.top
            anchors.topMargin: 65

            Text
            {
                id: stateText
                objectName: "ProductState";
                anchors.left: parent.left
                anchors.right: parent.right
                font: fontStyles.dashboardProductStateFont
                color: "black";
                function prepareProductStateText()
                {
                    if(context.hasLicenseNotification && context.shouldOverrideProductState)
					{
						if (context.licenseNotificationProductState != "")
	                        return context.licenseNotificationProductState;
					}
                    return tr.Localize(context.ProductStateDescription);
                }

				Connections
				{
					target: context;
					onProtectionStateDescriptionChanged: stateText.text = stateText.prepareProductStateText();
				}

                text: prepareProductStateText();
                elide: Text.ElideRight
            }

            Column
            {
                id: statuses
                anchors.left: stateText.left; anchors.leftMargin: 1
                anchors.right: parent.right; anchors.rightMargin: 20
                anchors.top: stateText.bottom
                anchors.topMargin: 11
                spacing: 12
                property real infoRowHeight: (statusImage.height * 0.7) / 5

                InfoRow // Active threats
                {
                    stateId : "ThreatsState"
                    itemId : tr.Localize("ThreatsState")
                    value : tr.Localize(context.ThreatsStateDescription)
                    height : statuses.infoRowHeight
                }

                InfoRow // Protection components
                {
                    stateId : "ProtectionState"
                    itemId : tr.Localize("ProtectionState")
                    value : tr.Localize(context.ProtectionStateDescription)
                    height : statuses.infoRowHeight
		    		canWrap : true
                }

                InfoRow // Bases
                {
                    stateId : "BasesState"
                    itemId : tr.Localize("BasesState")
                    function getBasesDescription()
                    {
                        if(EnumHelper.getKey(context, "UpdaterStatus") != "Idle")
                        {
                            return	tr.Localize(context.BasesStateDescription) + 
								(EnumHelper.getKey(context, "UpdaterStatus") == "RollbackRunning"
                                    ? tr.Localize("BasesRollbacking", {"BasesState_ProcessPercentCompleted" : context.BasesProcessPercentCompleted})
                                    : tr.Localize("BasesUpdating", {"BasesState_ProcessPercentCompleted" : context.BasesProcessPercentCompleted}));
                        }
                        return tr.Localize(context.BasesStateDescription);
                    }

                    value : getBasesDescription()
                    height : statuses.infoRowHeight
     				canWrap : true					
                }

                InfoRow // subscription
                {
					function hasValueText()
					{
						return context.LicenseStateDescription || context.hasLicenseNotification;
					}

                    function prepareSubscriptionText()
                    {
                        if(context.hasLicenseNotification)
                        {
                            var text = context.licenseNotificationLicenseState;
                            if(text != "")
                                return text;
                        }

                        return tr.Localize(context.LicenseStateDescription, {
                            "LicenseExpiredEx" : dashboard.getLicenseExpiredEx(),
                            "SubscriptionRemainingDays" : dashboard.getSubscriptionRemainingDays(),
                            "LicenseState_RemainingDaysCount": context.LicenseRemainingDaysCount
                            });
                    }
                    visible: context.IsSubscriptionLicense
                    itemId : tr.Localize("SubscriptionState")
                    stateId : "LicenseState"
					value : hasValueText() ? prepareSubscriptionText() : "";
                    height : statuses.infoRowHeight
					canWrap : true
                }

                InfoRow // Licenses
                {
					function hasValueText()
					{
						return context.LicenseStateDescription || context.hasLicenseNotification;
					}
                    function prepareLicenseText()
                    {
                        if(context.hasLicenseNotification)
                        {
                            var text = context.licenseNotificationLicenseState;
                            if(text != "")
                                return text;
                        }
                        return tr.Localize(context.LicenseStateDescription, {
                            "LicenseExpiredEx" : dashboard.getLicenseExpiredEx(),
                            "LicenseState_RemainingDaysCount": context.LicenseRemainingDaysCount
                            });
                    }
                    visible: !context.IsSubscriptionLicense
                    itemId : tr.Localize("LicenseState")
                    stateId : "LicenseState"
                    value : hasValueText() ? prepareLicenseText() : "";
                    height : statuses.infoRowHeight
					canWrap : true
                }

                InfoRow // Recommendations
                {
                    stateId : "RecommendationsState"
                    itemId : tr.Localize("RecommendationsState")
                    value : dashboard.getRecommendationsStateDescription()
                    visible: context.HasRecommendations;
                    height : statuses.infoRowHeight
                }
            }
        }
	}

    Widgets.Button
    {
        id: buyButton
	    focusRectEnabled: false

        Widgets.FocusImage
        {
            anchors.top: parent.top
            anchors.topMargin: 3
            anchors.right: parent.right
            anchors.rightMargin: 0
            tileHorizontally: true
            widthClipperSize: 10
            border {
                left: 105
                right: 5
            }
            source: "../common/images/focuses/focus_buy_button.png"
        }
        objectName: "buyButton"
        tabOrder: dashboardButton.tabOrder + 1
        property variant enumValue : context.BuyRenewType
        onEnumValueChanged: { visible = EnumHelper.getKey(context, "BuyRenewType") != "NoButton"; }
        anchors.right: dashboardButton.right
        anchors.top: dashboardButton.top
        anchors.topMargin: 16
        horizontalMargin: 20
        buttonHeight: 50
        backgroundImage: "../images/main_window/buy_normal.png"
        hoverImage: "../images/main_window/buy_hover.png"
        pressedImage: "../images/main_window/buy_pressed.png"
        text: dashboard.getButtonTypeName();
        textcolor: "#ffffff"
        textcolorDisabled: "#ffffff"
        minWidth: 128
        borderLeft: 106
        borderRight: 21
        borderWidth: 0
        textLeft: true
        leftMargin: 27
        textVerticalOffset: -1

        horizontalTileMode: BorderImage.Repeat
        verticalTileMode: BorderImage.Repeat

        textfont.pointSize: 14
        textfont.family: "FranklinGothicMediCondTT"
        textStyle: Text.Sunken
        textStyleColor: "#566f90"

        onClicked:
        {
			if(context.BuyRenewAction != undefined)
				context.BuyRenewAction.Invoke(Common.buildPath(parent)+"."+buyButton.objectName);
        }
    }
}
