package com.notificationFramework.notification;

import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.notificationFramework.sedentary.frontEnd.R;

/**
 * Created by pharmacy on 25/06/2018.
 */

public class FeedbackNotification extends SedentaryNotification {



    /**
     * Called when the service is started. Uses the Notification implementation of this method.
     * @param intent  the intent used to start the service
     * @param flags   additional information about the start request
     * @param startId the unique start id of this service
     * @return the return value of the Notification implementation of this method
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        snId = this.getResources().getInteger(R.integer.notffeedback_id);
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * Implementation of the abstract method specified in Notification which adds individual
     * properties to the notification which is to be sent such as the content title and content
     * text.
     *
     * @param builder the NotificationCompat.Builder provided to be altered with unique properties
     * @return the altered version of 'builder'
     */
    @Override
    protected NotificationCompat.Builder setParameters(NotificationCompat.Builder builder) {
        builder = super.setParameters(builder);
        builder.setContentTitle(this.getClass().getSimpleName());
        builder.setContentText(this.getString(R.string.feedback));
        if (preferences.getBoolean(getString(R.string.vibration), false)) {
            builder.setVibrate(new long[]{0, 500, 0, 500});
        }
        setCancellingAlarm();
        Log.d("Feedback", "Should be");
        return builder;
    }

    /**
     * Implementation of the abstract method specified in Notification which adds individual
     * properties to the NotificationChannel used if the API level is above 26.
     *
     * @param nc the NotificationChannel to be altered with individual properties
     * @return the altered version of NotificationChannel 'nc'
     */
    @TargetApi(26)
    protected NotificationChannel setChannelParameters(NotificationChannel nc) {
        nc = super.setChannelParameters(nc);
        nc.setDescription(this.getClass().getName());
        return nc;
    }

    private void setCancellingAlarm(){
        AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Log.d("Cancelling??", "hopefully");
        Intent i = new Intent(this,
                com.notificationFramework.notification.FeedbackNotificationCanceller.class);
        PendingIntent pi = PendingIntent.getBroadcast(this,
                R.integer.alarm_fnc, i, 0);
        SharedPreferences shared = this.getSharedPreferences(getString(
                R.string.preference_file_key), Context.MODE_PRIVATE);
        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 3000, pi);
    }
}