package com.ch.jedge.demo.glm.service;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import com.ch.jedge.glm.JGlmJava;
import com.ch.jedge.jbot.JBotJava;
import com.changhong.qlib.util.StringUtils;
import com.ch.jedge.demo.glm.R;

public abstract class BaseJedgeService extends IntentService {
    public static final String Service_Key_Mgs = "mgs";
    public static final String Service_Key_Coss = "coss";
    public static final String Service_Key_Multp = "multipanel";
    public static final String Service_Key_Moga = "moga";
    public static final String Service_Key_Vdalt = "vdalt";
    public static final String Service_Key_Awaker = "awaker";
    public static final String Service_Key_Gaffic = "gaffic";

    protected static final String EXTRA_DIR = "com.example.myapplication.extra.dir";
    protected static final String EXTRA_DIR_CACHE = "com.example.myapplication.extra.dir_cache";
    private static final String EXTRA_DIR_JAM = "com.example.myapplication.extra.jam";
    protected static final String ACTION_START_SERVICE = "com.example.myapplication.action.start";

    protected final String serviceKey;
    private String notificationId = "jedge";
    private String notificationName = "JedgeName";

    private static ThreadGroup group = new ThreadGroup("Services");


    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public BaseJedgeService(String name) {
        super(name);
        serviceKey = name;
    }

//    @Override
//    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
//        return START_REDELIVER_INTENT;
//    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    private Notification getNotification() {

        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Jedge服务")
                .setContentText("");

        //设置Notification的ChannelID,否则不能正常显示

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(notificationId);
        }
        Notification notification = builder.build();
        return notification;

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_START_SERVICE.equals(action)) {
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                //创建NotificationChannel
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                }

                startForeground(110, getNotification());

                final String dir = intent.getStringExtra(EXTRA_DIR);
                final String dir_cache = intent.getStringExtra(EXTRA_DIR_CACHE);
                final String jams = intent.getStringExtra(EXTRA_DIR_JAM);
//                if(StringUtils.isValidStr(jams))
//                    new Thread(group, () -> {
//                        handleStartService(dir, dir_cache, jams);
//                    },serviceKey, 4*1024*1024).start();
//                else
                    new Thread(group, () -> {
                        handleStartService(dir, dir_cache);
                    },serviceKey, 4*1024*1024).start();
//                startGPTService();
            }
        }
    }

    protected void handleStartService(String dir, String dir_cache, String jams) {};

    protected void handleStartService(String dir, String dir_cache) {};

    /**
     * Starts this service to perform action Foo with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */
    public static void startJedgeService(Context context, String dir, String dir_cache, Class<? extends BaseJedgeService> cls) {
        Intent intent = new Intent(context, cls);
        intent.setAction(ACTION_START_SERVICE);
        intent.putExtra(EXTRA_DIR, dir);
        intent.putExtra(EXTRA_DIR_CACHE, dir_cache);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    /**
     * Starts this service to perform action Foo with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */
    public static void startJedgeService(Context context, String dir, String dir_cache, String jams, Class<? extends BaseJedgeService> cls) {
        Log.d("JEDGE","try to start service"+cls.getName());
        Intent intent = new Intent(context, cls);
        intent.setAction(ACTION_START_SERVICE);
        intent.putExtra(EXTRA_DIR, dir);
        intent.putExtra(EXTRA_DIR_CACHE, dir_cache);
        intent.putExtra(EXTRA_DIR_JAM, jams);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

//    public static void startGPTService() {
//        String[] args = new String[1];
//        args[0] = "ppp";
//
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        new Thread(group, () -> {
//            JBotJava.jbot_main(args);
//        },"bot", 4*1024*1024).start();
//
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        new Thread(group, () -> {
//            JGlmJava.glm_main(args);
//        },"glm", 4*1024*1024).start();
//    }
}
