package com.sharefaith.thesharefaithapp.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sharefaith.thesharefaithapp.R;
import com.sharefaith.thesharefaithapp.base.SFAppData;
import com.sharefaith.thesharefaithapp.base.SFApplication;
import com.sharefaith.thesharefaithapp.base.SFConfig;

/**
 * Created by Anthony on 2016-1-25.
 *
 * Activity for displaying messages
 */
public class SFMessageActivity extends Activity
{
    private SFApplication mApplication;
    private SFConfig mConfig;
    private SFAppData mAppData;

    //private View mView;
    private TextView mContinueButton;

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        this.requestWindowFeature( Window.FEATURE_NO_TITLE );
        setContentView( R.layout.sf_leaving_app );

        //if not tablet keep screen from rotating
        if( !SFConfig.isTablet() )
        {
            setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
        }

        mApplication = SFApplication.getInstance();
        mAppData = new SFAppData();
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        //Google Analytics tracking
        mApplication.logScreen("link");

        mApplication.setCurrentActivity( this );
        mApplication.currentViewTag = "leavingapp";
        mApplication.currentSectionPosition = -1;

        mConfig = new SFConfig();
        loadInterface();
        mContinueButton =(TextView) findViewById( R.id.continue_button );
        mContinueButton.setBackgroundColor( Color.parseColor( "#" + mConfig.mNavColor ) );
        mContinueButton.setOnClickListener( new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                try
                {
                    Intent browser = new Intent( Intent.ACTION_VIEW, Uri.parse( mApplication.currentLink ) );
                    mApplication.logEvent( "leave_app","link" );
                    startActivity( browser );
                    finish();
                }
                catch( Exception e )
                {
                    Log.d( "URI", "Exception: " + e.getLocalizedMessage() );
                }
            }
        } );
    }

    @Override
    public void onBackPressed()
    {
        this.finish();
        overridePendingTransition( R.anim.sf_activity_slidein_left, R.anim.sf_activity_slideout_right );
    }

    /**
     * OnClickListener for the navigation bar
     * @param action
     * @return
     */
    private View.OnClickListener navItemListener( final String action )
    {
        return
                new View.OnClickListener()
                {
                    public void onClick( View v )
                    {
                        if( action.equals( "opennav" ) )
                        {
                            finish();
                            overridePendingTransition( R.anim.sf_activity_slidein_left, R.anim.sf_activity_slideout_right );
                        } // if( action.equals( "opennav" ) )
                        if( action.equals( "listennav" ) )
                        {

                            if( mApplication.isNavOpen )
                            {
                                SFSharedUIMethods.closeMenu();
                            }

                            Intent intent = new Intent( mApplication.getCurrentActivity(), SFDownloadsActivity.class  );
                            startActivity( intent );
                            overridePendingTransition( R.anim.sf_activity_slidein_right, R.anim.sf_activity_slideout_left );
                        } // if( action.equals( "opennav" ) )
                    }
                };
    }

    /**
     * Load the UI elements
     */
    private void loadInterface(  )
    {
        SFConfig config = new SFConfig();
        // Get the basic layout components going
        LinearLayout titleBarLayout = (LinearLayout) findViewById( R.id.navMenuContainer );
        titleBarLayout.setBackgroundColor( Color.parseColor( "#" + config.mNavColor ) );

        // Set the title bar properties
        TextView titleBarView = (TextView) findViewById( R.id.navMenuTitle );
        titleBarView.setText( config.mAppTitle );

        ImageButton openNav = (ImageButton) findViewById( R.id.openMenuImageButton );
        openNav.setOnClickListener( this.navItemListener( "opennav" ) );

        ImageButton listenNav = (ImageButton) findViewById( R.id.openListenImageButton );
        listenNav.setOnClickListener( this.navItemListener( "listennav" ) );

        layoutSetup();
    }

    private void layoutSetup()
    {
        ImageButton button = (ImageButton) findViewById( R.id.openMenuImageButton );
        button.setPadding( 0, mApplication.getDP( 10 ), 0, mApplication.getDP( 10 ) );
        button.setImageResource( R.drawable.sf_control_x_regular );
    }
}
