package com.sharefaith.thesharefaithapp.base;

/**
 * Created by Sharefaith1 on 2014-12-02.
 */
public class SFDemoTools
{
	/*
	public SFMainActivity mCaller;
	public AiDb mDb;
	public boolean mHadError = false;
	public int mTotalSize;
	public int mReceivedSize;
	public String mUserEmail;
	public String mWpSiteId;
	public String mSiteKey;

	public SFDemoTools( SFMainActivity caller )
	{
		this.mCaller = caller;
		this.mDb = SFMainActivity.db;
		this.mTotalSize = 0;
		this.mReceivedSize = 0;

		this.mWpSiteId = "-1";
		this.mSiteKey = "-1";
		this.mUserEmail = "";
	}

	public void getSiteKeys( String inputEmail )
	{
		this.mUserEmail = inputEmail;
		this.go();
	}


	// get the chain of events under way
	public void go()
	{
		String address = SFConstants.FIND_DEMO_URL + this.mUserEmail;

		DemoDownloadTask task = new DemoDownloadTask( this.mCaller, this );
		try
		{
			URL url = new URL( address );
			task.mNextAction = "getSiteId";
			task.execute( url );
		}
		catch( Exception e )
		{
			this.mHadError = true;
			String message = SFMainActivity.instance.getString( R.string.sync_error ) + e.getLocalizedMessage();
			SFMainActivity.makeToast( message );
		}
	}


	// parse the first bit of feedback (object IDs)
	public void parseJson( String input, String key )
	{
		try
		{
			JSONObject jObject = new JSONObject( input );
			HashMap<String,String> parsed = SFDemoTools.parseJson( jObject, new HashMap<String, String>() );


			for( String value : parsed.values() )
			{
				if( key.equals( "siteKey" ) )
				{
					this.mSiteKey = value;

					String address = SFConstants.FIND_DEMO_ID_URL + this.mSiteKey;

					DemoDownloadTask task = new DemoDownloadTask( this.mCaller, this );
					try
					{
						URL url = new URL( address );
						task.mNextAction = "finish";
						task.execute( url );
					}
					catch( Exception e )
					{
						this.mHadError = true;
						String message = SFMainActivity.instance.getString( R.string.sync_error ) + e.getLocalizedMessage();
						SFMainActivity.makeToast( message );
					}

					return;
				}
				else if( key.equals( "siteId" ) )
				{
					this.mWpSiteId = value;
					this.mCaller.gotDemoKeys( this.mSiteKey, this.mWpSiteId, this.mUserEmail );

					return;
				}
			}
		}
		catch( Exception e )
		{
			this.mSiteKey = "-1";
			this.mWpSiteId = "-1";
			e.printStackTrace();
			this.mCaller.hideProgress();
			return;
		}
	}


	public static HashMap<String,String> parseJson( JSONObject object, HashMap<String,String> input ) throws JSONException
	{
		Iterator<String> keys = object.keys();
		while( keys.hasNext() )
		{
			String key = keys.next();
			String stringValue = null;
			try
			{
				JSONObject value = object.getJSONObject( key );
				SFDemoTools.parseJson( value, input );
			}
			catch( Exception e )
			{
				stringValue = object.getString( key );
			}

			if( stringValue != null )
			{
				input.put( key, stringValue );
			}
		}


		return input;
	}

	private class DemoDownloadTask extends AsyncTask<URL, Integer, Long>
	{
		public SFMainActivity mCaller;
		public String mResult;
		public String mNextAction;
		public ArrayList<HashMap<String,String>> mSyncData;
		public DemoDownloadTask mSelfCaller;
		public boolean mHadError = false;
		public SFDemoTools mDirectCaller;

		public DemoDownloadTask( SFMainActivity caller, SFDemoTools directCaller )
		{
			this.mCaller = caller;
			this.mResult = "";
			this.mNextAction = "";
			this.mSyncData = new ArrayList<HashMap<String,String>>();
			this.mDirectCaller = directCaller;
		}

		@Override
		protected Long doInBackground( URL... urls )
		{
			try
			{
				BufferedReader in = new BufferedReader( new InputStreamReader( urls[0].openStream() ) );
				String line;

				while( ( line = in.readLine() ) != null )
				{
					if( line.equalsIgnoreCase( "bye" ) )
					{
						Log.e( "SOCKET ERROR", "Socket said goodbye" );
					}
					//get lines
					this.mResult += line;
				}
				in.close();
			}
			catch( Exception e )
			{
				this.mHadError = true;
				String message = SFMainActivity.instance.getString( R.string.sync_error ) + e.getLocalizedMessage();
				SFMainActivity.makeToast( message );
			}

			int count = urls.length;
			long totalSize = 0;
			for( int i = 0; i < count; i++ )
			{
				//totalSize += Downloader.downloadFile( urls[i] );
				publishProgress( (int) ((i / (float) count) * 100) );
				// Escape early if cancel() is called
				if( isCancelled() ) { break; }
			}
			return totalSize;
		}

		@Override
		protected void onProgressUpdate( Integer... progress )
		{
			//setProgressPercent(progress[0]);
		}

		// run the next piece of the chain
		@Override
		protected void onPostExecute( Long result )
		{
			final DemoDownloadTask callingTask = this;

			// onPostExecute runs on the main thread, which will pause the UI if we allow it to do so
			Thread t = new Thread(
				new Runnable()
				{
					public void run()
					{
						callingTask.runNextAction();
					}
				}

			);
			t.start();
		}

		public void runNextAction()
		{

			if( this.mHadError )
			{
				this.mCaller.hideProgress();
			}
			else if( this.mNextAction == null || this.mNextAction.equals( "getSiteId" ) )
			{
				this.mDirectCaller.parseJson( this.mResult, "siteKey" );
			}
			else if( this.mNextAction.equals( "finish" ) )
			{
				this.mDirectCaller.parseJson( this.mResult, "siteId" );
			}

		}

	} // private class DemoDownloadTask extends AsyncTask<URL, Integer, Long>
*/
}
