/*
 * Copyright (C) 2014 Jerrell Mardis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jerrellmardis.amphitheatre.task;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.jerrellmardis.amphitheatre.api.GuessItClient;
import com.jerrellmardis.amphitheatre.api.TMDbClient;
import com.jerrellmardis.amphitheatre.model.Movie;
import com.jerrellmardis.amphitheatre.model.tmdb.Config;
import com.jerrellmardis.amphitheatre.model.tmdb.Metadata;

import org.apache.commons.lang3.text.WordUtils;

import java.util.ArrayList;
import java.util.List;

import jcifs.smb.SmbFile;

/**
 * Created by Jerrell Mardis on 8/5/14.
 */
public class DownloadMovieInfoTask extends AsyncTask<Void, Void, Boolean> {

    private static final String SAMPLE = "sample";

    private String mDirectory;
    private List<Movie> mMovies;
    private List<SmbFile> mFiles;
    private OnTaskCompletedListener mOnTaskCompletedListener;

    public interface OnTaskCompletedListener {
        void onTaskCompleted();
    }

    public DownloadMovieInfoTask(String directory, List<SmbFile> files, OnTaskCompletedListener l) {
        mDirectory = directory;
        mFiles = files;
        mOnTaskCompletedListener = l;
        mMovies = new ArrayList<Movie>();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        Config config = TMDbClient.getConfig();

        for (SmbFile file : mFiles) {
            if (TextUtils.isEmpty(file.getPath()) || file.getName().toLowerCase().contains(SAMPLE)) {
                continue;
            }

            Movie movie = GuessItClient.guess(file.getName());

            // try to determine the movie's name based on its parent folder name if and only if
            // 1. The movie was not matched
            // or
            // 2. The movie title that was matched is equal to the filename (This is a special
            // case. Since my files are not named nicely, if the movie title that came back is equal
            // to the filename then that means that GuessIt didn't find a match)
            if (movie != null &&
                    (TextUtils.isEmpty(movie.getTitle()) || movie.getTitle().equals(file.getName()))) {

                String[] sections = file.getPath().split("/");
                String name = sections[sections.length - 2];

                if (!name.equals(mDirectory)) {
                    int indexOf = file.getPath().lastIndexOf(".");
                    String ext = file.getPath().substring(indexOf, file.getPath().length());
                    movie = GuessItClient.guess(name + ext);
                }
            }

            if (movie == null || TextUtils.isEmpty(movie.getTitle())) {
                if (movie == null) {
                    movie = new Movie();
                }

                movie.setTitle(WordUtils.capitalizeFully(file.getName()));
                movie.setVideoUrl(file.getPath());
                movie.setMatched(false);

                mMovies.add(movie);

                continue;
            }

            movie.setTitle(WordUtils.capitalizeFully(movie.getTitle()));

            if (!TextUtils.isEmpty(movie.getTitle())) {
                try {
                    Metadata metadata = TMDbClient.getMetadata(movie.getTitle(), movie.getYear());
                    movie.setCardImageUrl(config.getImages().getBase_url() + "original" + metadata.getResults().get(0).getPoster_path());
                    movie.setBackgroundImageUrl(config.getImages().getBase_url() + "original" + metadata.getResults().get(0).getBackdrop_path());
                    movie.settMDbId(metadata.getResults().get(0).getId());

                    if (movie.gettMDbId() != null) {
                        com.jerrellmardis.amphitheatre.model.tmdb.Movie tmDbMovie = TMDbClient.getMovie(movie.gettMDbId());
                        movie.setDescription(tmDbMovie.getOverview());
                        movie.setYear(tmDbMovie.getRelease_date());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            movie.setVideoUrl(file.getPath());
            movie.setMatched(true);

            mMovies.add(movie);
        }

        return true;
    }

    @Override
    protected void onPostExecute(Boolean processed) {
        Movie.saveInTx(mMovies);

        if (mOnTaskCompletedListener != null) {
            mOnTaskCompletedListener.onTaskCompleted();
        }
    }
}
@@ -3803,24 +3803,25 @@
 getYear());%0A
+%0A
             
@@ -3832,18 +3832,113 @@
     
-movie.setC
+if (metadata.getResults() != null && !metadata.getResults().isEmpty()) %7B%0A                        String c
 ardI
@@ -3936,33 +3936,35 @@
 ing cardImageUrl
-(
+ = 
 config.getImages
@@ -3986,32 +3986,64 @@
 ) + %22original%22 +
+%0A                               
  metadata.getRes
@@ -4068,27 +4068,30 @@
 oster_path()
-)
 ;%0A
+    
             
@@ -4111,26 +4111,78 @@
 .set
-Background
+CardImageUrl(cardImageUrl);%0A%0A                        String bg
 ImageUrl
 (con
@@ -4177,17 +4177,19 @@
 ImageUrl
-(
+ = 
 config.g
@@ -4227,16 +4227,48 @@
 ginal%22 +
+%0A                               
  metadat
@@ -4303,27 +4303,96 @@
 kdrop_path()
-)
 ;%0A
+                        movie.setBackgroundImageUrl(bgImageUrl);%0A%0A    
             
@@ -4449,24 +4449,46 @@
 0).getId());
+%0A                    %7D
 %0A%0A          

Fix IndexOutOfBoundsException when no movie art is found