package com.mints.goodmoney.manager;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;

import com.mints.goodmoney.MintsApplication;
import com.mints.goodmoney.R;
import com.mints.goodmoney.ui.activitys.MainActivity;
import com.mints.goodmoney.utils.SpanUtils;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * 通知管理
 */
public class NotifyManager {

    public static final String CHINALL_ID = "GOODMONEY";
    public static final String CHINALL_NAME = "好赚钱通知";

    private static NotifyManager _inst;
    private Activity activity;
    private RemoteViews remoteViews;
    private Notification notification;
    private NotificationManager manager;

    public static NotifyManager getInstance(Activity activity) {
        if (_inst != null) {
            return _inst;
        } else {
            _inst = new NotifyManager(activity);
            return _inst;
        }
    }

    private NotifyManager(Activity activity) {
        this.activity = activity;
        return;
    }

    /**
     * 创建通知
     *
     * @param water
     * @param step
     */
    public void createNotifyDialog(int water, int step) {
        manager = (NotificationManager) activity.getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel(CHINALL_ID, CHINALL_NAME, NotificationManager.IMPORTANCE_MAX);
        }
        notification = new NotificationCompat.Builder(activity, CHINALL_ID)
                .setOngoing(true)
                .setAutoCancel(true)
                .setContentTitle("")
                .setContentText("")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContent(getRemoteViews(water, step))
                .setVibrate(new long[]{0})
                .build();

        updateRemoteView(water, step);
    }

    /**
     * 创建通知渠道
     */
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.enableVibration(false);//震动不可用
        channel.setSound(null, null); //设置没有声
        NotificationManager notificationManager = (NotificationManager) activity.getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    /**
     * 创建通知显示view
     *
     * @param water
     * @param step
     * @return
     */
    private RemoteViews getRemoteViews(int water, int step) {
        PendingIntent mainPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
//        PendingIntent waterPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, WaterActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews = new RemoteViews(activity.getPackageName(), R.layout.remoteview);
//        remoteViews.setTextViewText(R.id.notify_step, "今日步数 " + step + " 步");
        remoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);
        remoteViews.setOnClickPendingIntent(R.id.notify_root, mainPendingIntent);
//        remoteViews.setOnClickPendingIntent(R.id.notify_drink, waterPendingIntent);
        return remoteViews;
    }

    /**
     * 更新通知
     *
     * @param water
     * @param step
     */
    public void updateRemoteView(int water, int step) {
        Context context = MintsApplication.getContext();
        if (remoteViews != null && context != null) {
            remoteViews.setTextViewText(R.id.notify_step,
                    new SpanUtils()
                            .append(step + "").setBold().setForegroundColor(context.getResources().getColor(R.color.main_mints))
                            .append("步 ︲ ").setForegroundColor(context.getResources().getColor(R.color.black_text))
                            .append(getDistanceByStep(step) + "").setBold().setForegroundColor(context.getResources().getColor(R.color.main_mints))
                            .append(" 公里").setForegroundColor(context.getResources().getColor(R.color.black_text))
                            .create());
//            remoteViews.setTextViewText(R.id.notify_step, step + "步 ︲ " + getDistanceByStep(step) + "公里");
            if (manager != null && notification != null) {
                manager.notify(1, notification);
            }
        }
    }

    /**
     * 计算公里数
     *
     * @param steps 步数
     * @return
     */
    private String getDistanceByStep(long steps) {
        return String.format("%.2f", (float) steps * 0.6F / 1000.0F);
    }
}
