/*******************************************************************************
 * 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.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.adobe.adobepass.accessenabler.models.Mvpd;
import com.adobe.primetime.reference.utils.StringUtils;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.manager.EntitlementManager;
import com.adobe.primetime.reference.utils.DownloadImageTask;

public class EntitlementDialogFragment extends DialogFragment
{
    private static final String MVPD_ARG = "mvpd_arg";
    private EntitlementDialogListener callback;
    private EntitlementManager.Status status;
    private String message;
    private Mvpd mvpd;

    /**
     * Creates a new instance of this dialog.
     *
     * @param status current entitlement flow status, used to decorate the dialog window.
     * @param msg the dialog message displayed to the user
     * @return a new instance of EntitlementDialogFragment
     */
    public static EntitlementDialogFragment newInstance(EntitlementManager.Status status, String msg)
    {
        return newInstance(status, msg, null);
    }

    public static EntitlementDialogFragment newInstance(EntitlementManager.Status status, String msg, Mvpd mvpd)
    {
        EntitlementDialogFragment dialog = new EntitlementDialogFragment();

        Bundle args = new Bundle();
        args.putSerializable("status", status);
        args.putString("msg", msg);
        if (mvpd != null)
        {
            args.putSerializable(MVPD_ARG, mvpd);
        }
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try
        {
            callback = (EntitlementDialogListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + " must implement EntitlementDialogListener.");
        }

    }

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

        status = (EntitlementManager.Status) getArguments().getSerializable("status");
        message = getArguments().getString("msg");
        mvpd = (Mvpd) getArguments().getSerializable(MVPD_ARG);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.entitlement_dialog, container, false);
        getDialog().setTitle(status.getTitle());

        // set text message displayed to user
        TextView tv = (TextView) view.findViewById(R.id.text);


        // setup dismiss dialog button
        Button button = (Button)view.findViewById(R.id.button_ok);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // call up owning activity
                callback.onDialogDismiss(status);
            }
        });

        // setup the logout button and logo only if user is currently authenticated
        Button btLogout = (Button)view.findViewById(R.id.button_logout);
        ImageView mvpdLogo = (ImageView) view.findViewById(R.id.mvpd_logo);

        if (EntitlementManager.Status.AUTHN_SUCCESS.equals(status))
        {
            // set success message
            tv.setText(status.getMessage());

            btLogout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // call up owning activity
                    callback.onDialogLogout(status);
                }
            });

            if (mvpd != null && !StringUtils.isEmpty(mvpd.getLogoUrl()))
            {
                new DownloadImageTask(mvpdLogo).execute(mvpd.getLogoUrl());
            }
            else
            {
                mvpdLogo.setVisibility(View.GONE);
            }

        }
        else
        {
            // only display the logout button and logo if the user is successfully logged in.
            btLogout.setVisibility(View.GONE);
            mvpdLogo.setVisibility(View.GONE);

            // set default text
            if (StringUtils.isEmpty(message))
            {
                tv.setText(status.getMessage());
            }
            else
            {
                tv.setText(message);
            }
        }

        return view;
    }

    /**
     * Called when the user cancels the dialog window, for example if the user clicks the device BACK button.
     * Treated the same as if the user selected the OK button by calling the parent activity's dismiss callback.
     *
     * @param dialog the dialog that was cancelled.
     * @see com.adobe.primetime.reference.ui.entitlement.EntitlementDialogFragment.EntitlementDialogListener#onDialogDismiss(com.adobe.primetime.reference.manager.EntitlementManager.Status)
     */
    @Override
    public void onCancel(DialogInterface dialog) {
        callback.onDialogDismiss(status);
    }

    // container activity must implement this interface
    public interface EntitlementDialogListener
    {
        /**
         * Called when the user dismisses the dialog window, either by clicking the 'OK' button or by clicking
         * the device BACK button.
         * @param status the status passed to the dialog when created.
         */
        public void onDialogDismiss(EntitlementManager.Status status);

        /**
         * Called when the user selects the 'Sign Out' button.
         * @param status the status passed to the dialog when created.
         */
        public void onDialogLogout(EntitlementManager.Status status);
    }

}
