package org.stepic.droid.receivers;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

import com.squareup.otto.Bus;

import org.stepic.droid.base.MainApplication;
import org.stepic.droid.events.video.VideoCachedOnDiskEvent;
import org.stepic.droid.model.CachedVideo;
import org.stepic.droid.model.DownloadEntity;
import org.stepic.droid.model.Lesson;
import org.stepic.droid.model.Step;
import org.stepic.droid.model.Unit;
import org.stepic.droid.preferences.UserPreferences;
import org.stepic.droid.store.ICancelSniffer;
import org.stepic.droid.store.IStoreStateManager;
import org.stepic.droid.store.operations.DatabaseFacade;
import org.stepic.droid.util.RWLocks;

import java.io.File;
import java.util.concurrent.ExecutorService;

import javax.inject.Inject;

public class DownloadCompleteReceiver extends BroadcastReceiver {

    @Inject
    UserPreferences mUserPrefs;
    @Inject
    DatabaseFacade mDatabaseFacade;
    @Inject
    IStoreStateManager mStoreStateManager;
    @Inject
    Bus bus;

    @Inject
    ICancelSniffer mCancelSniffer;

    @Inject
    ExecutorService mThreadSingleThreadExecutor;

    @Inject
    DownloadManager downloadManager;

    public DownloadCompleteReceiver() {
        MainApplication.component().inject(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        mThreadSingleThreadExecutor.execute(new Runnable() {
            @Override
            public void run() {
                blockForInBackground(referenceId);
            }
        });
    }

    private void blockForInBackground(final long referenceId) {
        try {
            RWLocks.DownloadLock.writeLock().lock();

            DownloadEntity downloadEntity = mDatabaseFacade.getDownloadEntityIfExist(referenceId);
            if (downloadEntity != null) {
                long video_id = downloadEntity.getVideoId();
                final long step_id = downloadEntity.getStepId();
                mDatabaseFacade.deleteDownloadEntityByDownloadId(referenceId);

                File downloadFolderAndFile = new File(mUserPrefs.getUserDownloadFolder(), video_id + "");
                String path = Uri.fromFile(downloadFolderAndFile).getPath();

                if (mCancelSniffer.isStepIdCanceled(step_id)) {
                    downloadManager.remove(referenceId);//remove notification (is it really work and need?)
                    mCancelSniffer.removeStepIdCancel(step_id);
                    Step step = mDatabaseFacade.getStepById(step_id);
                    if (step != null) {
                        Lesson lesson = mDatabaseFacade.getLessonById(step.getLesson());
                        if (lesson != null) {
                            Unit unit = mDatabaseFacade.getUnitByLessonId(lesson.getId());
                            if (unit != null && mCancelSniffer.isUnitIdIsCanceled(unit.getId())) {
                                mStoreStateManager.updateUnitLessonAfterDeleting(lesson.getId());//automatically update section
                                mCancelSniffer.removeUnitIdCancel(unit.getId());
                                if (mCancelSniffer.isSectionIdIsCanceled(unit.getSection())) {
                                    mCancelSniffer.removeSectionIdCancel(unit.getSection());
                                }
                            }
                        }
                    }
                } else {
                    //is not canceled
                    final CachedVideo cachedVideo = new CachedVideo(step_id, video_id, path, downloadEntity.getThumbnail());
                    cachedVideo.setQuality(downloadEntity.getQuality());
                    mDatabaseFacade.addVideo(cachedVideo);

                    final Step step = mDatabaseFacade.getStepById(step_id);
                    step.set_cached(true);
                    step.set_loading(false);
                    mDatabaseFacade.updateOnlyCachedLoadingStep(step);
                    mStoreStateManager.updateUnitLessonState(step.getLesson());
                    final Lesson lesson = mDatabaseFacade.getLessonById(step.getLesson());
                    Handler mainHandler = new Handler(MainApplication.getAppContext().getMainLooper());
                    //Say to ui that ui is cached now
                    Runnable myRunnable = new Runnable() {
                        @Override
                        public void run() {
                            if (lesson != null)
                                bus.post(new VideoCachedOnDiskEvent(step_id, lesson, cachedVideo));
                        }
                    };
                    mainHandler.post(myRunnable);
                }
            } else {
                downloadManager.remove(referenceId);//remove notification (is it really work and need?)
            }
        } finally

        {
            RWLocks.DownloadLock.writeLock().unlock();
        }
    }

}
@@ -225,33 +225,8 @@
 ler;
-%0Aimport android.util.Log;
 %0A%0Aim
@@ -795,23 +795,108 @@
 id.util.
-RWLocks
+AppConstants;%0Aimport org.stepic.droid.util.RWLocks;%0Aimport org.stepic.droid.util.StorageUtil
 ;%0A%0Aimpor
@@ -2139,16 +2139,22 @@
         
+final 
 long vid
@@ -2339,191 +2339,8 @@
 );%0A%0A
-                File downloadFolderAndFile = new File(mUserPrefs.getUserDownloadFolder(), video_id + %22%22);%0A                String path = Uri.fromFile(downloadFolderAndFile).getPath();%0A
 %0A   
@@ -3579,16 +3579,17 @@
 anceled%0A
+%0A
         
@@ -3604,110 +3604,1275 @@
     
-final CachedVideo cachedVideo = new CachedVideo(step_id, video_id, path, downloadEntity.getThumbnail()
+File userDownloadFolder = mUserPrefs.getUserDownloadFolder();%0A                    File downloadFolderAndFile = new File(userDownloadFolder, video_id + %22%22);%0A                    String path = Uri.fromFile(downloadFolderAndFile).getPath();%0A                    String thumbnail = downloadEntity.getThumbnail();%0A                    if (mUserPrefs.isSdChosen()) %7B%0A                        File sdFile = mUserPrefs.getSdCardDownloadFolder();%0A                        if (sdFile != null) %7B%0A                            StorageUtil.moveFile(userDownloadFolder.getPath(), video_id + %22%22, sdFile.getPath());%0A                            StorageUtil.moveFile(userDownloadFolder.getPath(), video_id + AppConstants.THUMBNAIL_POSTFIX_EXTENSION, sdFile.getPath());%0A                            downloadFolderAndFile = new File(sdFile, video_id + %22%22);%0A                            final File thumbnailFile = new File(sdFile, video_id + AppConstants.THUMBNAIL_POSTFIX_EXTENSION);%0A                            path = Uri.fromFile(downloadFolderAndFile).getPath();%0A                            thumbnail = Uri.fromFile(thumbnailFile).getPath();%0A                        %7D%0A%0A                    %7D%0A                    final CachedVideo cachedVideo = new CachedVideo(step_id, video_id, path, thumbnail
 );%0A 

move to sd card if sd card is chosen as primary storage