package com.ez.ertc.meeting.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.ez.ertc.meeting.ErtcEzmHelper;
import com.ez.ertc.meeting.ui.ErtcRoomActivity;

public class ErtcVideoMeetingService extends Service {

    public static final int SCREEN_RECORDER_CREATE = 1;
    public static final int CONFLUENCE_INIT = 2;

    public static final String SCREEN_PARAM_TYPE = "type";
    public static final String SCREEN_PARAM_CODE = "result_code";
    public static final String SCREEN_PARAM_REQUEST = "request_code";
    public static final String SCREEN_PARAM_DATA = "data";


    public static final String LAUNCH_FROM_NOTIFICATION = "launch_from_notification";

    //用于存储Activity退出后的数据
    private StoredData storedData = new StoredData();

    public static class StoredData{
        public int mRoomId;
        public String userId;
        public String sharedUserId;

        //自己当前的状态
        public boolean videoState;
        public boolean smallVideoState;
        public boolean audioState;
        public boolean shareSwitch;
    }

    class MyBinder extends Binder{
        public StoredData getData(){
            return storedData;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null){
            int type = intent.getIntExtra(SCREEN_PARAM_TYPE, -1);
            switch (type){
                case SCREEN_RECORDER_CREATE:
                    createScreenRecorder(intent);
                    break;
                case CONFLUENCE_INIT:
                    createVideoMeeting();
                    break;
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            stopForeground(true);
        }
    }

    @SuppressLint("NewApi")
    private void createVideoMeeting(){
        startForeground(110, createNotificationChannel());
    }

    private void createScreenRecorder(Intent intent){
        int resultCode = intent.getIntExtra(SCREEN_PARAM_CODE, -1);
        int requestCode = intent.getIntExtra(SCREEN_PARAM_REQUEST, -1);
        Intent data = intent.getParcelableExtra(SCREEN_PARAM_DATA);
        if(ErtcEzmHelper.ertcEngine != null) {
            ErtcEzmHelper.ertcEngine.onScreenShareActivityResult(requestCode, resultCode, data, this);
        }
    }

    @SuppressLint("NewApi")
    private Notification createNotificationChannel(){
        Notification.Builder builder = new Notification.Builder(getApplicationContext()); //获取一个Notification构造器
        Intent nfIntent = new Intent(getApplicationContext(), ErtcRoomActivity.class); //点击后跳转的界面，可以设置跳转数据
        nfIntent.putExtra(LAUNCH_FROM_NOTIFICATION, true);

        Bitmap appIcon = getAppIcon();
        builder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, nfIntent, PendingIntent.FLAG_CANCEL_CURRENT)) // 设置PendingIntent
                .setLargeIcon(appIcon) // 设置下拉列表中的图标(大图标)
                //.setContentTitle("SMI InstantView") // 设置下拉列表里的标题
//                .setSmallIcon(Icon.createWithBitmap(appIcon)) // 设置状态栏内的小图标
                .setContentText("正在会议中......") // 设置上下文内容
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

        /*以下是对Android 8.0的适配*/
        //普通notification适配
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("notification_id");
        }
        //前台服务notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel("notification_id", "录屏", NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(channel);
        }

        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        return notification;
    }

    // 通过包名获取对应的 Drawable 数据
    private Bitmap getAppIcon() {
        try {
            String packageName = getApplicationContext().getPackageName();
            PackageManager pm = getApplicationContext().getPackageManager();
            ApplicationInfo info = pm.getApplicationInfo(packageName, 0);
            Drawable appIconDrawable = info.loadIcon(pm);
            return drawableToBitmap(appIconDrawable);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的长宽
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        // 取 drawable 的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;

    }
}
