/*******************************************************************************
 * ADOBE CONFIDENTIAL
 *  ___________________
 *
 *  Copyright 2014 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 *  NOTICE:  All information contained herein is, and remains
 *  the property of Adobe Systems Incorporated and its suppliers,
 *  if any.  The intellectual and technical concepts contained
 *  herein are proprietary to Adobe Systems Incorporated and its
 *  suppliers and are protected by trade secret or copyright law.
 *  Dissemination of this information or reproduction of this material
 *  is strictly forbidden unless prior written permission is obtained
 *  from Adobe Systems Incorporated.
 ******************************************************************************/

package com.adobe.primetime.reference.ui.entitlement;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.adobe.primetime.reference.PrimetimeReference;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.manager.EntitlementManager;

public class MvpdLoginActivity extends Activity implements EntitlementManager.ProviderWebView.ProviderWebViewListener
{
    /* Bundle key name to use when passing target URL as argument */
    public static final String EXTRA_TARGET_URL = "targetUrl";

    private static final String LOG_TAG = PrimetimeReference.LOG_APP_NAME + "MvpdLoginActivity";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // retrieve the name of the MVPD from the intent that started the activity
        Intent intent = getIntent();
        String targetUrl = intent.getStringExtra(EXTRA_TARGET_URL);

        // inflate the layout for this activity
        setContentView(R.layout.entitlement_provider_login);

        // get references to UI elements
        WebView loginWebView = (WebView) findViewById(R.id.provider_login_webview);

        // setup client
        EntitlementManager.ProviderWebView webViewClient = new EntitlementManager.ProviderWebView(this);
        loginWebView.setWebViewClient(webViewClient);

        // enable JavaScript support
        WebSettings browserSettings = loginWebView.getSettings();
        browserSettings.setJavaScriptEnabled(true);
        browserSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        setTitle(R.string.entitlement_login_activity_title);
        loginWebView.loadUrl(targetUrl);
    }

    @Override
    public void onFinish()
    {
        Intent result = new Intent();
        setResult(RESULT_OK, result);
        finish();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        /* Declaring activity in manifest as handling configuration changes has
         desired side-effect of preventing activity reload, preventing
         excessive calls to content provider's authentication endpoint.
         */
        super.onConfigurationChanged(newConfig);
    }

}
