package oko.tm.oko_pro_classic;


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.StrictMode;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class TCP_client extends Service {

    final String LOG_TAG = "myLogs";
    final String SERVICE_NAME = "TCP_client";

    public static Context mContext;

    public void onCreate() {
        super.onCreate();
        mContext=this;
        Log.d(LOG_TAG, SERVICE_NAME + " Service onCreate");

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        new Thread(new ClientThread(this)).start();

    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(LOG_TAG, SERVICE_NAME + " Service_onStartCommand");

        //MainTask();
        //return super.onStartCommand(intent, flags, startId);
        readFlags(flags);
        //MyRun mr = new MyRun(startId);
        //new Thread(mr).start();
        Log.d(LOG_TAG, SERVICE_NAME + " #" + startId + " create");
        return START_STICKY; // START_STICKY; //Это значит, что сервис будет восстановлен после убийства.
        /*Both codes are only relevant when the phone runs out of memory and kills the service before it finishes executing.
        START_STICKY tells the OS to recreate the service after it has enough memory and call onStartCommand()
        again with a null intent. START_NOT_STICKY tells the OS to not bother recreating the service again.
        There is also a third code START_REDELIVER_INTENT that tells the OS to recreate the service AND redelivery the same intent
        to onStartCommand().
         */
    }

    public void onDestroy() {
        super.onDestroy();

        try {
            Intent intent1 = new Intent(ClientThread.CLIENT_BROADCAST_RECIEVE_COMMAND);
            intent1.putExtra("command", "");
            intent1.putExtra("close_socket", true);
            LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d(LOG_TAG, SERVICE_NAME + " Service_onDestroy");

    }

    public IBinder onBind(Intent intent) {
        Log.d(LOG_TAG, SERVICE_NAME + " Service_onBind");
        return null;
    }

    void readFlags(int flags) {
        if ((flags&START_FLAG_REDELIVERY) == START_FLAG_REDELIVERY)
            Log.d(LOG_TAG, SERVICE_NAME + " START_FLAG_REDELIVERY");
        if ((flags&START_FLAG_RETRY) == START_FLAG_RETRY)
            Log.d(LOG_TAG, SERVICE_NAME + " START_FLAG_RETRY");
    }

    class MyRun implements Runnable {

        int startId;

        public MyRun(int startId) {
            this.startId = startId;
            Log.d(LOG_TAG, SERVICE_NAME + " MyRun#" + startId + " create");
        }

        public void run() {
            Log.d(LOG_TAG, SERVICE_NAME + " MyRun#" + startId + " start");
            try {
                //TimeUnit.SECONDS.sleep(15);
            } catch (Exception e) {
                //e.printStackTrace();
            }
            //stop();
        }

        void stop() {
            Log.d(LOG_TAG, SERVICE_NAME + " MyRun#" + startId + " end, stopSelfResult("
                    + startId + ") = " + stopSelfResult(startId));
        }
    }



}
