package com.genlot.ninglottery.toolkit;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

import com.genlot.nativesdk.SystemNI;
import com.genlot.nativesdk.sender.EventToken;
import com.genlot.ninglottery.application.LotteryApplication;

import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;

public class AppTool {

    private static final String SERVER_VERSION = "system/LT-MS01/version.txt";

    /**
     * 获得app VersionName
     * @param context Context
     * @return  VersionName
     */
    public static String getAppVersionName(Context context) {
        try {
            PackageInfo packInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获得app versionCode
     * @param context Context
     * @return  versionCode
     */
    public static int getAppVersionCode(Context context) {
        try {
            PackageInfo packInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return packInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 获得服务器 VersionName
     * @return  VersionName
     */
    public static String getServerVersion() {
        String localFile = SystemNI.getDownloadPath() + "/version.txt";
        int result = SystemNI.downloadFile(SERVER_VERSION, localFile, 0);
        if (result != 0) {
            return "获取服务器版本失败！";
        }
        try {
            File file = new File(localFile);
            FileInputStream fis = new FileInputStream(file);
            byte[] data = new byte[1024];
            int length = fis.read(data);
            return new String(data, 0, length);
        } catch (Exception e) {
            e.printStackTrace();
            return "获取服务器版本失败！";
        }
    }

    /**
     * 比较版本
     * @param serverVer 服务器版本
     * @param localVer  本地版本
     * @return  boolean
     */
    public static boolean compareVersion(String serverVer, String localVer) {
        try {
            String[] tmp = serverVer.split("\\.");
            String[] tmp2 = localVer.split("\\.");
            int serverMV = Integer.parseInt(tmp[0]);
            int localMV2 = Integer.parseInt(tmp2[0]);
            if (serverMV > localMV2) {
                return true;
            } else if (serverMV < localMV2) {
                return false;
            }
            serverMV = Integer.parseInt(tmp[1]);
            localMV2 = Integer.parseInt(tmp2[1]);
            if (serverMV > localMV2) {
                return true;
            } else if (serverMV < localMV2) {
                return false;
            }

            serverMV = Integer.parseInt(tmp[2].substring(0, 1));
            localMV2 = Integer.parseInt(tmp2[2]);
            return serverMV > localMV2;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断当前是否有可用网络
     *
     * @param context   Context
     * @return  boolean
     */
    public static boolean isNetworkAvalible(Context context) {
        // 获得网络状态管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null) {
            return false;
        } else {
            // 建立网络数组
            NetworkInfo[] net_info = connectivityManager.getAllNetworkInfo();
            if (net_info != null) {
                for (NetworkInfo networkInfo : net_info) {
                    // 判断获得的网络状态是否是处于连接状态
                    if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    /**
     * 设置系统时间
     *
     * @param millis    毫秒
     */
    public static boolean setSystemTime(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        LogTool.logD("设置系统时间：" + calendar.get(Calendar.YEAR) + "-" +
                calendar.get(Calendar.MONTH) + "-" +
                calendar.get(Calendar.DAY_OF_MONTH) +"-"+
                calendar.get(Calendar.HOUR_OF_DAY) + "-" +
                calendar.get(Calendar.MINUTE)+ "-"+
                calendar.get(Calendar.SECOND));
        try {
            if (LotteryApplication.getInstance().getSystemTool() != null) {
                LotteryApplication.getInstance().getSystemTool().setTime(calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
                        calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
                return true;
            } else {
                LogTool.logE("getSystemTool = null !");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
