/*
 * 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.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import butterknife.Bind;
import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.task.Task;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.adapter.DownloadAdapter;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMultiBinding;
import com.arialyy.simple.dialog.DownloadNumDialog;
import com.arialyy.simple.module.DownloadModule;

/**
 * Created by Lyy on 2016/9/27.
 */
public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
  @Bind(R.id.list)    RecyclerView mList;
  @Bind(R.id.toolbar) Toolbar      mBar;
  DownloadAdapter mAdapter;

  @Override protected int setLayoutId() {
    return R.layout.activity_multi;
  }

  @Override protected void init(Bundle savedInstanceState) {
    super.init(savedInstanceState);
    setSupportActionBar(mBar);
    mBar.setTitle("多任务下载");
    mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadData());
    mList.setLayoutManager(new LinearLayoutManager(this));
    mList.setAdapter(mAdapter);
  }

  //private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  //  long len = 0;
  //
  //  @Override public void onReceive(Context context, Intent intent) {
  //    String         action = intent.getAction();
  //    DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
  //    switch (action) {
  //      case DownloadManager.ACTION_PRE:
  //        L.d(TAG, "download pre");
  //        mAdapter.updateState(entity);
  //        break;
  //      case DownloadManager.ACTION_POST_PRE:
  //        len = entity.getFileSize();
  //        L.d(TAG, "download post pre");
  //        break;
  //      case DownloadManager.ACTION_START:
  //        L.d(TAG, "download start");
  //        break;
  //      case DownloadManager.ACTION_RESUME:
  //        L.d(TAG, "download resume");
  //        long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1);
  //        mAdapter.updateState(entity);
  //        break;
  //      case DownloadManager.ACTION_RUNNING:
  //        long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
  //        long speed = intent.getLongExtra(DownloadManager.CURRENT_SPEED, 0);
  //        //mAdapter.setProgress(entity.getDownloadUrl(), current, speed);
  //        mAdapter.setProgress(entity);
  //        break;
  //      case DownloadManager.ACTION_STOP:
  //        L.d(TAG, "download stop");
  //        mAdapter.updateState(entity);
  //        break;
  //      case DownloadManager.ACTION_COMPLETE:
  //        L.d(TAG, "download complete");
  //        mAdapter.updateState(entity);
  //        break;
  //      case DownloadManager.ACTION_CANCEL:
  //        L.d(TAG, "download cancel");
  //        mAdapter.updateState(entity);
  //        break;
  //      case DownloadManager.ACTION_FAIL:
  //        L.d(TAG, "download fail");
  //        break;
  //    }
  //  }
  //};

  public void onClick(View view){
    switch (view.getId()){
      case R.id.num:
        DownloadNumDialog dialog = new DownloadNumDialog(this);
        dialog.show(getSupportFragmentManager(), "download_num");
        break;
      case R.id.stop_all:
        Aria.get(this).stopAllTask();
        break;
    }
  }

  @Override protected void onResume() {
    super.onResume();
    Aria.whit(this).addSchedulerListener(new MySchedulerListener());
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    //unregisterReceiver(mReceiver);
  }

  @Override protected void dataCallback(int result, Object data) {
    super.dataCallback(result, data);
    if (result == DownloadNumDialog.RESULT_CODE){
      mAdapter.setDownloadNum(Integer.parseInt(data + ""));
    }
  }

  private class MySchedulerListener extends AMTarget.SimpleSchedulerListener{
    @Override public void onTaskPre(Task task) {
      super.onTaskPre(task);
      L.d(TAG, "download pre");
      mAdapter.updateState(task.getDownloadEntity());
    }

    @Override public void onTaskStart(Task task) {
      super.onTaskStart(task);
      L.d(TAG, "download start");
      mAdapter.updateState(task.getDownloadEntity());
    }

    @Override public void onTaskResume(Task task) {
      super.onTaskResume(task);
      L.d(TAG, "download resume");
      mAdapter.updateState(task.getDownloadEntity());
    }

    @Override public void onTaskRunning(Task task) {
      super.onTaskRunning(task);
      mAdapter.setProgress(task.getDownloadEntity());
    }

    @Override public void onTaskStop(Task task) {
      super.onTaskStop(task);
      mAdapter.updateState(task.getDownloadEntity());
    }

    @Override public void onTaskCancel(Task task) {
      super.onTaskCancel(task);
      mAdapter.updateState(task.getDownloadEntity());
    }

    @Override public void onTaskComplete(Task task) {
      super.onTaskComplete(task);
      mAdapter.updateState(task.getDownloadEntity());
    }

    @Override public void onTaskFail(Task task) {
      super.onTaskFail(task);
      L.d(TAG, "download fail");
    }
  }
}