package com.android.chmo.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.android.chmo.R;
import com.android.chmo.ui.activity.order.MyOrderActivity;



public class NotifyUtils {
    private Context context;
    private NotificationManager mNotificationManager;

    public NotifyUtils(Context context) {
        this.context = context;
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void showOrderNotification(String ticker, String title, String content, int id) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setContentTitle(title)
                .setContentText(content)
                .setContentIntent(getDefalutIntent(id,Notification.FLAG_AUTO_CANCEL))
                .setTicker(ticker)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setSmallIcon(R.mipmap.ic_launcher);
        Notification notify = mBuilder.build();
        notify.flags = Notification.FLAG_AUTO_CANCEL;

        mNotificationManager.notify(id, notify);
    }

    public PendingIntent getDefalutIntent(int id, int flags) {
        Intent intent = new Intent(context, MyOrderActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("from","notify");
        // requestCode 不能固定，否则参数无法传递
        PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent, flags);
        return pendingIntent;
    }

    public void clearAll() {
        mNotificationManager.cancelAll();
    }

}
