//
//  DateTextLib.swift
//  jmarine
//
//  Created by JRCE0064Shinagawa on 2021/06/04.
//  Copyright © 2021 J-MarineCloud. All rights reserved.
//

//import UIKit

import Foundation
class DateTextLib: NSObject {
    
    /// Date型をグレゴリオ暦日付文字列にする
    // 有効期限表示で使う
    static func Date2CalendarText(_ date: Date ) -> String {
        
        let outformatter = DateFormatter()
        outformatter.calendar = Calendar(identifier: .gregorian)    // グレゴリオ暦
        outformatter.locale = Locale(identifier: "ja_JP")       // 年月日並び順
        outformatter.timeZone = TimeZone(identifier: "Asia/Tokyo")  // タイムゾーン
        outformatter.dateStyle = .short
        outformatter.timeStyle = .none
        let labelString = outformatter.string(from: date )
        
        return labelString
    }
    
    /// Date型を時刻文字列にする
    // 見守り者のデータ更新時刻表示、アメダス表示で使う
    static func Date2TimeText(_ time: Date, isSec : Bool ) -> String {
        
        let outformatter = DateFormatter()
        outformatter.calendar = Calendar(identifier: .gregorian)    // グレゴリオ暦
        outformatter.locale = Locale(identifier: "ja_JP")       // 年月日並び順
        outformatter.timeZone = TimeZone(identifier: "Asia/Tokyo")  // タイムゾーン
        outformatter.dateStyle = .none
        if isSec {
            outformatter.timeStyle = .medium
        }
        else {
            outformatter.timeStyle = .short
        }
        
        let labelString = outformatter.string(from: time )
        
        return labelString
    }
    /// TimeInterval 型を時刻文字列にする
    static func TimeInterval2TimeText(_ diffsec : TimeInterval ) -> String {
        if diffsec < 0.0 {
            return "00:00:00"
        }
        
        var tgtsec = diffsec
        if diffsec >= 100.0 * 3600.0 {
            tgtsec = diffsec.truncatingRemainder(dividingBy: (100.0*3600.0) )
        }
        let allsecond = Int(tgtsec)
        let h = allsecond / 3600
        let m = (allsecond / 60) % 60
        let s = allsecond % 60
        
        let retstring = String( format:"%02d:%02d:%02d", h, m, s )
        return retstring
    }
    /// Date型をISO8601日付文字列にする
    // サーバとの通信に使う
    static func Date2ISO8601Text(_ date: Date ) -> String {
        let formatter = ISO8601DateFormatter()
        let dateString = formatter.string(from: date)
        return dateString
    }
    /// ISO8601文字列をDate型に変換
    static func ISO8601Text2Date(_ dateString: String? ) -> Date? {
        let formatter = ISO8601DateFormatter()
        let setDate = formatter.date(from: dateString ?? "")
        return setDate
    }

    /// unixTime(整数値,秒)を最終的にISO8601文字列にする
    static func UnixTime2ISO8601Text(_ unixTime: Int64 ) -> String {
        let formatter = ISO8601DateFormatter()
        let timeInterval = TimeInterval(integerLiteral: unixTime )
        let unixDate = Date(timeIntervalSince1970: timeInterval)
        let unixStr = formatter.string(from: unixDate )
        return unixStr
    }

    /// ISO8601文字列をunixint(整数値,秒)に変換
    static func ISO8601Text2UnixTime(_ unixStr: String? ) -> Int64 {
        let formatter = ISO8601DateFormatter()
        var unixTime : Int64 = 0
        if let setDate = formatter.date(from: unixStr ?? "") {
            let unixinterval :Double = setDate.timeIntervalSince1970
            unixTime = Int64(unixinterval)
        }
        return unixTime
    }
    
    /// unixTime(整数値,秒)をDate型に変換
    static func UnixTime2Date(_ unixTime:Int64) -> Date{
        let timeInterval = TimeInterval(integerLiteral: unixTime )
        let date = Date(timeIntervalSince1970: timeInterval)
        return date
    }
    
    ///Date型 をunixTime(整数値,秒)に変換
    static func Date2UnixTime(date: Date)->Int64{
        return Int64(date.timeIntervalSince1970)
    }
    
    /// Date型を年月日のBCD文字列にする
    static func Date2BCDText(_ date: Date)->String {
        let BCDdate = Date2BCD(date)
        let retText = String(BCDdate)
        return retText
    }
    /// Date型を年月日のBCDにする
    static func Date2BCD(_ date: Date)->Int{
        var calendar = Calendar.current
        calendar.timeZone = TimeZone.current
        let datecompps = calendar.dateComponents([.year, .month, .day], from: date )
        let BCDdate : Int = datecompps.year! * 10000 + datecompps.month! * 100 + datecompps.day!
        return BCDdate
    }

    /// Date型をFormatの日付文字列にする
    static func Date2FormatText(_ date: Date, format: String ) -> String {
        
        let outformatter = DateFormatter()
        outformatter.calendar = Calendar(identifier: .gregorian)    // グレゴリオ暦
        outformatter.locale = Locale(identifier: "ja_JP")       // 年月日並び順
        outformatter.timeZone = TimeZone(identifier: "Asia/Tokyo")  // タイムゾーン
        outformatter.dateFormat = format
        let labelString = outformatter.string(from: date )
        
        return labelString
    }
    
    ///ISO8601文字列をFormatの日付文字列にする
    static func ISO86012FormatText(_ date: String, format: String, errFormat: String) -> String{
        if let date = ISO8601Text2Date(date){
            return Date2FormatText(date, format: format)
        }else{
            return errFormat
        }
    }
}
