package com.virjar.echo.adr;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.virjar.echo.adr.repo.EchoConfig;
import com.virjar.echo.adr.ui.MainActivity;
import com.virjar.echo.nat.client.EchoClient;
import com.virjar.echo.nat.log.EchoLogger;

import java.util.concurrent.atomic.AtomicBoolean;

import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;

public class HttpProxyService extends Service {

    private EchoClient echoClient;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        startService();
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startService();
        return START_STICKY;
    }

    private AtomicBoolean serviceStarted = new AtomicBoolean(false);

    private void startService() {
        if (serviceStarted.compareAndSet(false, true)) {
            startServiceInternal();
        }
    }

    private void setNotifyChannel() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return;
        }
        NotificationChannel notificationChannel = new NotificationChannel(BuildConfig.APPLICATION_ID,
                "channel", NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.YELLOW);
        notificationChannel.setShowBadge(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (manager == null) {
            return;
        }
        manager.createNotificationChannel(notificationChannel);
    }

    private void startServiceInternal() {

        setNotifyChannel();

        Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
        // 设置PendingIntent
        Intent nfIntent = new Intent(this, MainActivity.class);
        builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, FLAG_UPDATE_CURRENT))
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) // 设置下拉列表中的图标(大图标)
                .setContentTitle("EchoProxy")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("代理服务agent")
                .setWhen(System.currentTimeMillis());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(BuildConfig.APPLICATION_ID);
        }

        Notification notification = builder.build();
        notification.defaults = Notification.DEFAULT_SOUND;
        startForeground(110, notification);

        EchoLogger.getLogger().info("start EchoProxy front service");

        echoClient = new EchoClient(
                EchoConfig.getEchoServer(), EchoConfig.getEchoServerPort(),
                EchoConfig.getClientId());
        new Thread() {
            @Override
            public void run() {
                //因为会访问网络，所以需要在新线程
                echoClient.startUp();
            }
        }.start();

    }

}
