package com.sheep.gamegroup.util;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.TaskEty;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.io.File;

import rx.functions.Action1;

import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;

/**
 * Created by realicing on 2018/5/3.
 * realicing@sina.com
 */
public class TestUtil {

    public static void setDebugDownLoad(final Context context, View view, final TaskEty taskEty) {
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                final String[] items = {"复制", "系统下载", "浏览器下载"};
                AlertDialog dialog = new AlertDialog.Builder(context).setTitle("请选择操作")
                        .setItems(items, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (items[which]){
                                    case "复制下载地址":
                                        debugCopy(context, taskEty);
                                        break;
                                    case "系统下载":
                                        debugSysDownload(context, taskEty);
                                        break;
                                    case "浏览器下载":
                                        debugWebDownload(context, taskEty);
                                        break;
                                }
                            }
                        }).create();
                dialog.show();
                return true;
            }
        });
    }

    private static void debugCopy(final Context context, final TaskEty taskEty) {
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if(clipboardManager != null && !TextUtils.isEmpty(taskEty.getDownload_link())) {
            //创建ClipData对象
            ClipData clipData = ClipData.newPlainText("sheep text copy", taskEty.getDownload_link());
            //添加ClipData对象到剪切板中
            clipboardManager.setPrimaryClip(clipData);
            G.showToast("已经复制游戏下载链接");
        }
    }

    private static void debugSysDownload(final Context context, TaskEty taskEty) {
        sysDownload(context, taskEty.getDownload_link(), taskEty.getTask_name(), "下载小绵羊任务中的apk："+taskEty.getPackage_names(), taskEty.getPackage_names()+".apk", "系统开始下载apk");
    }

    public static void sysDownload(final Context context, String downloadLink, String title, String description, String fileName, String toast) {
        // 创建下载请求
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadLink));

        /*
         * 设置在通知栏是否显示下载通知(下载进度), 有 3 个值可选:
         *    VISIBILITY_VISIBLE:                   下载过程中可见, 下载完后自动消失 (默认)
         *    VISIBILITY_VISIBLE_NOTIFY_COMPLETED:  下载过程中和下载完成后均可见
         *    VISIBILITY_HIDDEN:                    始终不显示通知
         */
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        // 设置通知的标题和描述
        request.setTitle(title);
        request.setDescription(description);

        /*
         * 设置允许使用的网络类型, 可选值:
         *     NETWORK_MOBILE:      移动网络
         *     NETWORK_WIFI:        WIFI网络
         *     NETWORK_BLUETOOTH:   蓝牙网络
         * 默认为所有网络都允许
         */
//                     request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);

        // 添加请求头
        // request.addRequestHeader("User-Agent", "Chrome Mozilla/5.0");
        if(fileName != null) {
            // 设置下载文件的保存位置
            File saveFile = new File(DIR, fileName);
            request.setDestinationUri(Uri.fromFile(saveFile));
        }

        /*
         * 2. 获取下载管理器服务的实例, 添加下载任务
         */
        DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        if(manager != null) {
            // 将下载请求加入下载队列, 返回一个下载ID
            long downloadId = manager.enqueue(request);
            if(toast != null)
                G.showToast(toast);
        }
    }
    private static void debugWebDownload(final Context context, final TaskEty taskEty) {
        webDownload(context, taskEty.getDownload_link());
    }
    public static void webDownload(final Context context, String downloadLink) {
        if(context instanceof Activity) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse(downloadLink));
            context.startActivity(intent);
            G.showToast("浏览器开始下载apk");
        }
    }

    public static void testChange(final Activity activity, TextView test_change){
        if(BuildConfig.DEBUG || !"sheep".equals(BuildConfig.FLAVOR)){
            test_change.setVisibility(View.VISIBLE);
            test_change.setText(SheepApp.getInstance().getConnectAddress().getName());
            test_change.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final ConnectAddress[] connectAddresses = ConnectAddress.values();
                    AlertDialog dialog = new AlertDialog.Builder(activity).setTitle("请选择要切换的服务器")
                            .setSingleChoiceItems(new ArrayAdapter<>(activity, android.R.layout.simple_list_item_single_choice, connectAddresses), SheepApp.getInstance().getConnectAddress().ordinal(), new DialogInterface.OnClickListener(){

                                @Override
                                public void onClick(DialogInterface dialogInterface, int which) {
                                    SheepApp.getInstance().setConnectAddress(connectAddresses[which]);
                                }
                            })
                            .create();
                    dialog.show();
                }
            });
        }
    }

    public static void test(Activity activity) {
        Jump2View.getInstance().goWeb(activity, "https://m.leluncp.com/?tag_from=10800", "测试");
    }
}
