package com.sharefaith.thesharefaithapp.base;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.appideas.base.AiDb;
import com.appideas.base.AiSTr;
import com.sharefaith.thesharefaithapp.R;

import io.sentry.Sentry;

/**
 * Created by Sharefaith1 on 2014-12-18.
 */
public class SFPushHandler
{
	public int mChannel;
	public String mPayload;
	private SFApplication mApplication;
	private AiDb mDb;

	public SFPushHandler( Context context )
	{
		mApplication = SFApplication.getInstance();
		this.mChannel = -1;
		this.mPayload = "";
        mDb = mApplication.getDb();
	}

	// if there's a push notification to be handled, will grab it
	public SFPushHandler getPush()
	{
		//why? SFPushHandler returnValue = new SFPushHandler(mContext);

		AiDb db = this.mDb;
		String sql =
				"SELECT		lookup_key, lookup_value " +
				"FROM		config_lookup " +
				"WHERE		lookup_key = 'pushchannel' OR " +
				"			lookup_key = 'pushpayload'";
		String[][] result = db.query( sql );
		for( int i = 0; i < result.length; i++ )
		{
			String key = AiSTr.denullifyString( result[ i ][ 0 ] );
			String value = AiSTr.denullifyString( result[ i ][ 1 ] );
			if( key.equals( "pushchannel" ) )
			{
				this.mChannel = Integer.valueOf( value );
			}
			else if(  key.equals( "pushpayload" ) )
			{
				this.mPayload = value;
			}
		}

		return this;
	}

	public void prepareIncoming( Bundle incoming )
	{
		//String messageText = incoming.getString( "message", "" );
		//String messageTitle = incoming.getString( "title", "" );
		String messageChannel = incoming.getString( "channel", "" );
		//String messageUrl = incoming.getString( "url", "" );

		try
		{
			if (Integer.valueOf( messageChannel ) == mApplication.getResources().getInteger( R.integer.push_channel_simple ) )
			{
				this.consumeSimplePush( incoming );
			} else
			{
				this.consumeDeepPush( incoming );
			}

		}
		catch(Exception e)
		{
			Log.e( "mTag", "ERROR: Could not handle push" );
			Sentry.capture(e);
		}
	}

	public void consumeSimplePush( Bundle incoming )
	{
		String messageText = "";
		String messageUrl = incoming.getString( "url", "" );

		if( messageUrl.length() > 0 )
		{
			messageText = messageUrl;
		}

		this.savePush( mApplication.getResources().getInteger( R.integer.push_channel_simple ), messageText );
	}

	public void consumeDeepPush( Bundle incoming )
	{
		String messageId = incoming.getString( "id", "" );

		Integer incomingChannel = Integer.valueOf( incoming.getString( "channel", "0" ) );
		if( incomingChannel < mApplication.getResources().getInteger( R.integer.push_channel_sermon ) || incomingChannel > mApplication.getResources().getInteger( R.integer.push_channel_post ) )
		{
			return;
		}

		if( messageId.length() < 1 )
		{
			messageId = "0";
		}

		this.savePush( incomingChannel, messageId );
	}

	public void clearPush()
	{
		// There are times when SFPushHandler gets instantiated before SFMainActivity is all the way ready
		if( this.mDb == null )
		{
			//SFMainActivity mainActivity = SFMainActivity.instance;
			this.mDb = mApplication.getDb();
		}

		// blindly delete anything that's there already
		String sql =
				"DELETE FROM	config_lookup " +
				"WHERE			lookup_key = 'pushchannel' OR " +
				"				lookup_key = 'pushpayload'";
		this.mDb.noReturnQuery( sql );
	}

	// save the fact that a push message was received.
	public void savePush( int channel, String payload )
	{
		this.clearPush();

		String sql =
				"INSERT INTO	config_lookup " +
				"				( lookup_key, lookup_value ) " +
				"VALUES			( 'pushchannel', '" + String.valueOf( channel ) + "' )";
		this.mDb.noReturnQuery( sql );

		sql =
				"INSERT INTO	config_lookup " +
				"				( lookup_key, lookup_value ) " +
				"VALUES			( 'pushpayload', '" + AiSTr.dbEscape( payload ) + "' )";
		this.mDb.noReturnQuery( sql );
	}

}
