package com.wjf.dynamicapploader.activity;

import android.app.DownloadManager;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.morgoo.droidplugin.pm.PluginManager;
import com.wjf.dynamicapploader.R;
import com.wjf.dynamicapploader.adapter.ApkListAdapter;
import com.wjf.dynamicapploader.adapter.ApkOperator;
import com.wjf.dynamicapploader.eventbus.DownloadEvent;
import com.wjf.dynamicapploader.model.ApkItem;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * description
 *
 * @author weijianfeng @Hangzhou Youzan Technology Co.Ltd
 * @date 16/8/29
 */
public class PluginStoreActivity extends AppCompatActivity{

    @Bind(R.id.list_rv_recycler)
    RecyclerView mRvRecycler;

    private ApkListAdapter mStoreAdapter; // 适配器


    // 服务连接
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override public void onServiceConnected(ComponentName name, IBinder service) {
            loadApks();
        }

        @Override public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_plugin);

        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        initView();

        //临时实现下
        downloadPlugin();

        //initPlugin();
    }

    private void downloadPlugin() {
        DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
        String apkUrl = "http://cdn.sinacloud.net/apkbucket/app-debug.apk?KID=sina,rer1va47xccivHywUxLc&Expires=1472554493&ssig=%2BwsFEmCk3H";
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.apk");
        long downloadId = downloadManager.enqueue(request);
    }

    private void initView() {

        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        mRvRecycler.setLayoutManager(llm);

        mStoreAdapter = new ApkListAdapter(this, ApkOperator.TYPE_STORE);
        mRvRecycler.setAdapter(mStoreAdapter);
    }

    private void initPlugin() {
        if (PluginManager.getInstance().isConnected()) {
            loadApks();
        } else {
            PluginManager.getInstance().addServiceConnection(mServiceConnection);
        }
    }

    // 加载Apk
    private void loadApks() {
        // 异步加载, 防止Apk过多, 影响速度
        Observable.just(getApkFromDownload())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ArrayList<ApkItem>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArrayList<ApkItem> apkItems) {
                        mStoreAdapter.setApkItems(apkItems);
                    }
                });
    }

    // 从下载文件夹获取Apk
    private ArrayList<ApkItem> getApkFromDownload() {
        File files = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        PackageManager pm = this.getPackageManager();
        ArrayList<ApkItem> apkItems = new ArrayList<>();
        for (File file : files.listFiles()) {
            if (file.exists() && file.getPath().toLowerCase().endsWith(".apk")) {
                final PackageInfo info = pm.getPackageArchiveInfo(file.getPath(), 0);
                apkItems.add(new ApkItem(pm, info, file.getPath()));
            }
        }
        return apkItems;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
        PluginManager.getInstance().removeServiceConnection(mServiceConnection);
    }

    @Subscribe
    public void onEvent(DownloadEvent event) {
        initPlugin();
    }
}

@@ -1949,24 +1949,26 @@
 nitView();%0A%0A
+//
         //%E4%B8%B4%E6%97%B6
@@ -1971,16 +1971,18 @@
 //%E4%B8%B4%E6%97%B6%E5%AE%9E%E7%8E%B0%E4%B8%8B%0A
+//
         
@@ -2008,18 +2008,16 @@
         
-//
 initPlug
@@ -2196,116 +2196,55 @@
 p://
-cdn.sinacloud.net/apkbucket/app-debug.apk?KID=sina,rer1va47xccivHywUxLc&Expires=1472554493&ssig=%252BwsFEmCk3H
+pluginapk-plugin.stor.sinaapp.com/app-debug.apk
 %22;%0A 

change plugin apk download url.