package ru.bitel.bgbilling.modules.tv.dyn.tv24h;

import bitel.billing.server.util.Config;
import ru.bitel.bgbilling.modules.tv.server.bean.enums.ModuleSetupKeys;
import ru.bitel.bgbilling.server.util.ModuleSetup;
import ru.bitel.bgbilling.server.util.Setup;

public class Tv24hConf
	extends Config
{
    public enum SubscriptionMode
    {
        create,
        renew;

        public static SubscriptionMode valueOf( int id )
        {
            switch( id )
            {
                case 1:
                    return create;

                case 0:
                default:
                    return renew;
            }
        }
    }
    
    final boolean agentMode;
    final SubscriptionMode subscriptionMode;
    
	final int contractPatternId;
	final int tvAccountSpecId;

	final int paramUserId;
	final int paramNameId;
	final int paramLastNameId;
	final int paramFirstNameId;
	final int paramEmailId;
	final int paramPhoneId;

	final int chargeTypeId;
	final int paymentTypeId;
	
	final boolean providerFree;

	/**
	 * Нужно ли создавать новый договор и аккаунт, если пришёл запрос
	 * и не смогли найти договор с таким номером телефона
	 */
	final boolean isNeedCreateNewContract;
	final boolean isNeedUpdatePhoneOnContract;
	final boolean enableSubscriptionPause;

	final int inetId;
	final String providerURL;

	public Tv24hConf( int moduleId, ModuleSetup moduleSetup, Setup serverSetup )
	{
        this.agentMode = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_AGENT_MODE ) > 0;
        this.subscriptionMode = SubscriptionMode.valueOf( moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_SUBSCRIPTION_MODE ) );

		this.contractPatternId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_CONTRACT_PATTERN_ID );
		this.tvAccountSpecId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_TV_ACCOUNT_SPEC_ID );

		this.paramUserId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_PARAM_USER_ID );

		this.paramNameId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_PARAM_NAME_ID );
		this.paramLastNameId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_PARAM_LAST_NAME_ID );
		this.paramFirstNameId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_PARAM_FIRST_NAME_ID );

		this.paramEmailId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_PARAM_EMAIL_ID );
		this.paramPhoneId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_PARAM_PHONE_ID );

		this.chargeTypeId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_CHARGE_TYPE_ID );
		this.paymentTypeId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_PAYMENT_TYPE_ID );
		
		this.providerFree = moduleSetup.getBoolean( ModuleSetupKeys.OM_TV24H_PROVIDER_FREE );
		this.isNeedCreateNewContract = moduleSetup.getBoolean( ModuleSetupKeys.OM_TV24H_NEED_CREATE_NEW_CONTRACT );
		this.isNeedUpdatePhoneOnContract = moduleSetup.getBoolean( ModuleSetupKeys.OM_TV24H_NEED_UPDATE_PHONE );
		this.inetId = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_INET_MODULE_ID );
		//разрешено ли приостанавливать подписки, возможно, которые уже были оплачены
		this.enableSubscriptionPause = moduleSetup.getInt( ModuleSetupKeys.OM_TV24H_ENABLE_SUB_PAUSE ) > 0;
		this.providerURL = getApiUrl( moduleSetup );
	}

	static public String getApiUrl( ModuleSetup moduleSetup )
	{
	    return moduleSetup.get( ModuleSetupKeys.OM_TV24H_PROVIDER_URL );
	}
}