package dp.mendelu.roman.diplomkaapp42;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

/**
 * Sluzba pro synchronizaci dat.
 */
public class SyncServiceApp extends Service {
    public int flag = 0;
    private Looper mServiceLooper;
    private ServiceHandler mServiceHandler;
    private String TAG = "SyncServiceApp";
    private int pom = 0;
    Service mService;

    // Handler that receives messages from the thread
    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }
        @Override
        public void handleMessage(Message msg) {
            // Normally we would do some work here, like download a file.
            // For our sample, we just sleep for 5 seconds.

            long endTime = System.currentTimeMillis() + 5*1000;
            /*
            while (System.currentTimeMillis() < endTime) {
                synchronized (this) {
                    try {
                        wait(endTime - System.currentTimeMillis());
                        Log.i(TAG, "wait");

                    } catch (Exception e) {
                    }
                }
            }
            */
            //testovaci nekonecna smycka

            while (flag == 0) synchronized (this) {
                try {

                    Log.i(TAG, "wait");
                    //problem s predanim this, zadna instance vytvorena "bokem" nefungovala
                    //this je tam potreba pro otevreni databaze
                    new AsyncLoadJSONDoc(getApplicationContext()).execute();
                    wait(60000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            /*
            synchronized (this) {
                try {
                    wait(30000);
                    new AsyncLoadJSONDoc(getApplicationContext()).execute();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }*/
            Log.i(TAG, "Repeat");
            //handleMessage(msg);

            // Stop the service using the startId, so that we don't stop
            // the service in the middle of handling another job
            stopSelf(msg.arg1);

        }
    }

    @Override
    public void onCreate() {
        // Start up the thread running the service.  Note that we create a
        // separate thread because the service normally runs in the process's
        // main thread, which we don't want to block.  We also make it
        // background priority so CPU-intensive work will not disrupt our UI.

        //Process.THREAD_PRIORITY_BACKGROUND;
        Log.i(TAG, "Starting service");


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

        HandlerThread thread = new HandlerThread ("ServiceStartArguments",startId);
        thread.start();
        // Get the HandlerThread's Looper and use it for our Handler
        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);

        // For each start request, send a message to start a job and deliver the
        // start ID so we know which request we're stopping when we finish the job
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;

        mServiceHandler.sendMessage(msg);

        //return?sticky If we get killed, after returning from here,
        //meaning that the service should be
        //restarted via a call to onStartCommand(), supplying the same Intent as
        //was delivered this time

        return START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null



        return null;
    }

    @Override
    public void onDestroy() {
        flag = 1;
        Log.i(TAG, "Service done");
        Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();


    }
}