package com.tofly.ghrq_route.common.util.date;

import android.content.Context;

import com.tofly.ghrq_route.common.util.net.NetUtil;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具
 * Created by zombiexq on 2016/7/6.
 */
public class TimeUtil {

    // 获得当天0点时间
    public static Date getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    //获取当天24点时间
    public static Date getTimesnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    //字符串 如(2016-07-06 10:00:03)转换时间戳
    public static long strtotime(String str) {

        return 0;
    }

    //判断某一时间是否在今天之内
    public static boolean isToday(Date date) {
        long begintime = getTimesmorning().getTime();
        long endtime = getTimesnight().getTime();
        long datetime = date.getTime();

        if(datetime>begintime&&datetime<endtime){
            return true;
        }else{
            return false;
        }
    }

    //获取网络时间
    private static Date getWebsiteDatetime(Context context) {

        if (NetUtil.getNetworkState(context)!=NetUtil.NETWORN_NONE) {
            try {
                URL url = new URL("http://www.baidu.com");// 取得资源对象
                URLConnection uc = url.openConnection();// 生成连接对象
                uc.connect();// 发出连接
                long ld = uc.getDate();// 读取网站日期时间
                Date date = new Date(ld);// 转换为标准时间对象
             //   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);// 输出北京时间
            //    return sdf.format(date);
                return date;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /*根据时间秒换算成小时,分钟和秒N*/
    public static String secondToString(long second) {
        final int hour = 60 * 60;
        final int minute = 60;
        String strResult = "";
        if (second >= hour){
            long h = second / hour;
            long s = second % hour;
            if (s >= minute){
                long m = s / minute;
                long ss = s % minute;
                strResult = h + "时" + m + "分" + ss + "秒";
            }else {
                strResult = h + "时" + 0 + "分" + s + "秒";
            }
        }else if (second > minute){
            long m = second / minute;
            long ss = second % minute;
            strResult = m + "分" + ss + "秒";
        }else {
            strResult = second + "秒";
        }
        return strResult;
    }

    /*根据时间秒换算成小时,分钟和秒N*/
    public static String audioSecondToString(long second) {
        final int hour = 60 * 60;
        final int minute = 60;
        String strResult = "";
        if (second >= hour){
            long h = second / hour;
            long s = second % hour;
            if (s >= minute){
                long m = s / minute;
                long ss = s % minute;
                strResult = h + "'" + m + "''" + ss + "'''";
            }else {
                strResult = h + "'" + 0 + "''" + s + "'''";
            }
        }else if (second > minute){
            long m = second / minute;
            long ss = second % minute;
            strResult = m + "'" + ss + "''";
        }else {
            strResult = second + "'";
        }
        return strResult;
    }
}
