//
//  SHUserAccountManager.swift
//  CallRecording
//
//  Created by Mazy on 2020/7/13.
//  Copyright © 2020 Mazy. All rights reserved.
//

import Foundation
import SwiftyJSON

@objc class SHUserAccountManager: NSObject {
    
    /// singleton
    @objc static let shared = SHUserAccountManager()
    
    /// private init for singleton
    private override init() {}
    
    // info model
    @objc var userInfo: CRUserInfoModel?
    
    var h5_url: JSON = JSON.null
    var isPayfront: Bool = false
    var isStartFront: Bool = false
    var autoButtonText: String = ""
    var threeDayText: String = ""
    
    @objc var h5_urlDic : Dictionary<String, Any>{
        return h5_url.dictionaryObject!
    }
    
    var servicePhone: String?
    
    @objc var isMember: Bool {
        
//        return true
        
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: "en_CN")
        dateFormatter.timeZone = TimeZone.current
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
        
        if let dateString = CRUserDefaults.expireInTime, let expiryDate = dateFormatter.date(from: dateString) {
            logWarning("expiryDate == \(expiryDate)")
            logWarning("currentDate == \(Date())")
            if expiryDate.timeIntervalSince(Date()) > 0 {
                return true
            } else {
                return false
            }
        }
        
        return userInfo?.isMember ?? false
    }
    
    func logout() {
        userInfo = nil
        CRUserDefaults.accessToken = nil
        CRUserDefaults.accessUid = nil
    }
    
    func getUserInfo(_ success: @escaping((CRUserInfoModel)->Void), failure: @escaping ()->Void) {
        
        CMNetworkManager.shared.postRequest(withPath: .touristLogin) { (json, error) in
            CRUserDefaults.accessToken = json["result"]["data"]["token"].stringValue
            CMNetworkManager.shared.getRequest(withPath: .getUserInfo) { (json, error) in
                if error == nil {
                    let infoModel = CRUserInfoModel(json: json["result"]["data"])
                    self.userInfo = infoModel
                    CRUserDefaults.accessUid = infoModel.uid
                    CRUserDefaults.accessToken = infoModel.token
                    success(infoModel)
                } else {
                    failure()
                }
            }
         }   
    }
    
    func logCollection(with action: CMLogCollecAction, value: String = "") {
        let params = ["event": "guazinovel", "action": action.rawValue, "value": value]
        CMNetworkManager.shared.postLogRequest(withPath: .logCollection, parameters: params) { (json, error) in
        }
    }
    
    func getNovelInfo(_ success: @escaping((JSON)->Void)) {
        CMNetworkManager.shared.getRequest(withPath: .novelInfo) { (result, error) in
            if result["status"].intValue == 200 {
                self.h5_url         = result["result"]["data"]["h5_url"]
                self.isStartFront   = result["result"]["data"]["open1close0"]["isPayfront"].boolValue
                self.autoButtonText = result["result"]["data"]["config"]["pay_button_text"].stringValue
                self.threeDayText   = result["result"]["data"]["config"]["pay_3day_txt"].stringValue
                success(result["result"]["data"])
            }
        }
    }
}
