package com.viaccessorca.voplayerdemo.downloader;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v7.app.NotificationCompat;

import com.viaccessorca.voplayerdemo.R;
import com.viaccessorca.voplayerdemo.activities.MediaListActivity;
import com.viaccessorca.voplayerdemo.downloader.DownloaderWrapper.OnErrorListener;
import com.viaccessorca.voplayerdemo.downloader.DownloaderWrapper.OnInfoListener;

public class DownloadService extends Service implements
        OnInfoListener,
        OnErrorListener {


    private DownloadManager downloadManager;

    public DownloadService() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        downloadManager = DownloadManager.getInstance(getApplicationContext());
        if (downloadManager != null) {
            downloadManager.registerOnInfoListener(this);
            downloadManager.registerOnErrorListener(this);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {

        if (downloadManager != null) {
            downloadManager.unregisterOnInfoListener(this);
            downloadManager.unregisterOnErrorListener(this);
        }
        super.onDestroy();
    }

    @Override
    public boolean onInfo(DownloaderWrapper downloader, int what, int extra) {

        switch (what) {
            case DownloadManager.DOWNLOADER_INFO_DOWNLOAD_COMPLETED:;
                sendNotification(downloader.getUri(), getString(R.string.service_download_finsih));
                break;

            case DownloadManager.DOWNLOADER_INFO_DOWNLOAD_PROGRESS:
                break;
        }
        return false;
    }

    @Override
    public boolean onError(DownloaderWrapper downloader, int what, int extra) {

        String value;
        switch (what) {
            case DownloadManager.DOWNLOADER_ERROR_NOMORESPACE:
                value = getString(R.string.service_download_error_space);
                break;
            case DownloadManager.DOWNLOADER_ERROR_NETWORK_BROKEN:
                value = getString(R.string.service_download_error_network);
                break;
            case DownloadManager.DOWNLOADER_ERROR_OFFLINE_NOT_ALLOWED:
                value = getString(R.string.service_download_error_ona);
                break;
            case DownloadManager.DOWNLOADER_ERROR_LIVE_UNSUPPORTTED:
                value = getString(R.string.service_download_error_live);
                break;
            default:
                value = "Error Unknown";
        }
        if (downloadManager != null) {
            sendNotification(downloader.getUri(), value);
            downloadManager.releaseDownloaderForMediaId(downloader.getMediaId());
        }
        return false;
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void sendNotification(String updatedUri, String value) {

        //Creating Notification Builder
        NotificationCompat.Builder notification = new NotificationCompat.Builder(DownloadService.this);
        notification.setContentTitle(getString(R.string.app_name));
        notification.setContentText(value);
        notification.setSmallIcon(R.drawable.icon_nbg);
        notification.setAutoCancel(true);
        notification.setDefaults(Notification.DEFAULT_SOUND);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(DownloadService.this);
        stackBuilder.addParentStack(MediaListActivity.class);

        Intent resultIntent = new Intent(DownloadService.this, MediaListActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

        stackBuilder.addNextIntent(resultIntent);
        PendingIntent pIntent =  stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        notification.setContentIntent(pIntent);

        NotificationManager manager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, notification.build());
    }
}
