/*
 * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
 *
 * 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.arialyy.simple.download.service_download;

import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.frame.util.show.T;

/**
 * Created by Aria.Lao on 2017/4/5.
 * 在服务中使用 Aria进行下载
 */
public class DownloadService extends Service {

  private static final String DOWNLOAD_URL =
      //"http://kotlinlang.org/docs/kotlin-docs.pdf";
      //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
      //"http://static.gaoshouyou.com/d/21/e8/61218d78d0e8b79df68dbc18dd484c97.apk";
      //不支持断点的链接
      "http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
  private DownloadNotification mNotify;

  @Nullable @Override public IBinder onBind(Intent intent) {
    return null;
  }

  @Override public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
  }

  @Override public void onCreate() {
    super.onCreate();
    mNotify = new DownloadNotification(getApplicationContext());
    Aria.download(this).addSchedulerListener(new MySchedulerListener());
    Aria.download(this)
        .load(DOWNLOAD_URL)
        .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/service_task.apk")
        .start();
  }

  @Override public void onDestroy() {
    super.onDestroy();
    Aria.download(this).removeSchedulerListener();
  }

  private class MySchedulerListener extends Aria.DownloadSchedulerListener {

    @Override public void onNoSupportBreakPoint(DownloadTask task) {
      super.onNoSupportBreakPoint(task);
      T.showShort(getApplicationContext(), "该下载链接不支持断点");
    }

    @Override public void onTaskStart(DownloadTask task) {
      T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + "，开始下载");
    }

    @Override public void onTaskResume(DownloadTask task) {
      super.onTaskResume(task);
    }

    @Override public void onTaskStop(DownloadTask task) {
      T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + "，停止下载");
    }

    @Override public void onTaskCancel(DownloadTask task) {
      T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + "，取消下载");
    }

    @Override public void onTaskFail(DownloadTask task) {
      T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + "，下载失败");
    }

    @Override public void onTaskComplete(DownloadTask task) {
      T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + "，下载完成");
      mNotify.upload(100);
    }

    @Override public void onTaskRunning(DownloadTask task) {
      long len = task.getFileSize();
      int p = (int) (task.getCurrentProgress() * 100 / len);
      mNotify.upload(p);
    }
  }
}
