/*
 * Copyright (C) 2016 Olmo Gallegos Hernández.
 *
 * 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 es.voghdev.pdfviewpager.library;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

import java.io.File;

import es.voghdev.pdfviewpager.library.remote.DownloadFile;
import es.voghdev.pdfviewpager.library.remote.DownloadFileUrlConnectionImpl;
import es.voghdev.pdfviewpager.library.util.FileUtil;

public class RemotePDFViewPager extends ViewPager implements DownloadFile.Listener {
    protected Context context;
    protected DownloadFile.Listener listener;

    public RemotePDFViewPager(Context context, String pdfUrl, DownloadFile.Listener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        init(pdfUrl);
    }

    public RemotePDFViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }

    private void init(String pdfUrl) {
        DownloadFile downloadFile = new DownloadFileUrlConnectionImpl(context, new Handler(), this);
        downloadFile.download(pdfUrl,
                new File(context.getCacheDir(), FileUtil.extractFileNameFromURL(pdfUrl)).getAbsolutePath());
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a;

            a = context.obtainStyledAttributes(attrs, R.styleable.PDFViewPager);
            String pdfUrl = a.getString(R.styleable.PDFViewPager_pdfUrl);

            if (pdfUrl != null && pdfUrl.length() > 0) {
                init(pdfUrl);
            }

            a.recycle();
        }
    }

    @Override
    public void onSuccess(String url, String destinationPath) {
        listener.onSuccess(url, destinationPath);
    }

    @Override
    public void onFailure(Exception e) {
        listener.onFailure(e);
    }

    @Override
    public void onProgressUpdate(int progress, int total) {
        listener.onProgressUpdate(progress, total);
    }

    public class NullListener implements DownloadFile.Listener {
        public void onSuccess(String url, String destinationPath) {
        }

        public void onFailure(Exception e) {
        }

        public void onProgressUpdate(int progress, int total) {
        }
    }
}

@@ -1144,24 +1144,65 @@
 xt context;%0A
+    protected DownloadFile downloadFile;%0A
     protecte
@@ -1427,32 +1427,353 @@
 r;%0A        init(
+new DownloadFileUrlConnectionImpl(context, new Handler(), this), pdfUrl);%0A    %7D%0A%0A    public RemotePDFViewPager(Context context, DownloadFile downloadFile, String pdfUrl, DownloadFile.Listener listener) %7B%0A        super(context);%0A        this.context = context;%0A        this.listener = listener;%0A        init(downloadFile, 
 pdfUrl);%0A    %7D%0A%0A
@@ -1954,16 +1954,43 @@
 id init(
+DownloadFile downloadFile, 
 String p
@@ -2006,24 +2006,27 @@
         
+set
 Download
 File dow
@@ -2009,37 +2009,35 @@
      setDownload
-File 
+er(
 downloadFile = n
@@ -2036,73 +2036,8 @@
 File
- = new DownloadFileUrlConnectionImpl(context, new Handler(), this
 );%0A 
@@ -2527,66 +2527,239 @@
 nit(
-pdfUrl);%0A            %7D%0A%0A            a.recycle();%0A        %7D
+new DownloadFileUrlConnectionImpl(context, new Handler(), this), pdfUrl);%0A            %7D%0A%0A            a.recycle();%0A        %7D%0A    %7D%0A%0A    public void setDownloader(DownloadFile downloadFile) %7B%0A        this.downloadFile = downloadFile;
 %0A   

Add constructor that receives a DownloadFile object