//
//  YHAIGreetCollectionReusableView.swift
//  galaxy
//
//  Created by Dufet on 2024/11/27.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import Lottie

class YHAIGreetCollectionReusableView: UICollectionReusableView {
        
    static let reuseIdentifier: String = "YHAIGreetCollectionReusableView"
    
    let horizonalGap = 16.0
    
    lazy var iconImgView: LottieAnimationView! = {
        let lottieView = LottieAnimationView(name: "gangxiaobao_logo")
        lottieView.loopMode = .loop
        lottieView.contentMode = .scaleAspectFit
        lottieView.play()
        return lottieView
    }()
    
    lazy var titleLabel:UILabel = {
        let lable = UILabel()
        lable.textColor = UIColor.mainTextColor
        lable.textAlignment = .left
        lable.font = UIFont(name: "AlimamaShuHeiTi-Bold", size: 30)
        lable.text = "Hi, 早上好!"
        return lable
    }()
    
    lazy var subtitleLabel:UILabel = {
        let lable = UILabel()
        lable.textColor = UIColor.init(hex:0x6D788A)
        lable.textAlignment = .left
        lable.font = UIFont.PFSC_R(ofSize:14)
        lable.numberOfLines = 0
        lable.text = "港小宝来啦！超懂香港身份规划，畅聊香港生活，快来和我一起欢乐探索香港！"
        return lable
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateGreetingText() {
        titleLabel.text = greetingBasedOnTime()
    }
    
    func createUI() {
        
        self.addSubview(iconImgView)
        self.addSubview(titleLabel)
        self.addSubview(subtitleLabel)
     
        iconImgView.snp.makeConstraints { make in
            make.right.equalTo(-20+horizonalGap)
            make.top.equalTo(4)
            make.width.equalTo(120)
            make.height.equalTo(94)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(24-horizonalGap)
            make.bottom.equalTo(iconImgView.snp.bottom).offset(-15)
            make.height.equalTo(42)
        }
        
        subtitleLabel.snp.makeConstraints { make in
            make.left.equalTo(24-horizonalGap)
            make.right.equalTo(-24+horizonalGap)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
        }

    }
    
    func greetingBasedOnTime() -> String {
        
        let currentDate = Date()
        let calendar = Calendar.current
        let hour = calendar.component(.hour, from: currentDate)
        let minute = calendar.component(.minute, from: currentDate)
        
        var result = "Hi，"

        switch (hour, minute) {
        case (5..<8, _):
            result += "早上好"
        case (8, 0..<30):
            result += "早上好"
        case (8, 30..<60):
            result +=  "上午好"
        case (8..<11, _):
            result +=  "上午好"
        case (11..<14, _):
            result +=  "中午好"
        case (14..<18, _):
            result +=  "下午好"
        case (18..<24, _):
            result +=  "晚上好"
        case (0..<5, _):
            result +=  "晚上好"
        default:
            result +=  "晚上好" // 默认情况
        }
        result += "!"
        return result
    }
    
}
