//
//  QSLVipGoodCollectionViewCell.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/11/28.
//

import UIKit

class QSLVipGoodCollectionViewCell: UICollectionViewCell {
    
    var goodModel: QSLGoodModel?
    
    lazy var bgView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .white
        view.addRadius(radius: 8.rpx)
        view.addBorder(borderWidth: 2.rpx, borderColor: .hexStringColor(hexString: "#EEEEEE"))
        return view
    }()
    
    lazy var tagIcon: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "vip_goods_tag_icon")
        return imageView
    }()
    
    lazy var goodNameLabel: UILabel = {
        
        let label = UILabel()
        label.text("年度会员")
        label.boldFont(13)
        label.textColor = .hexStringColor(hexString: "#404040")
        return label
    }()
    
    lazy var goodDailyPriceLabel: UILabel = {
        
        let label = UILabel()
        label.textAlignment = .center
        label.addRadius(radius: 12.rpx)
        label.backgroundColor = .hexStringColor(hexString: "#FFFEFC")
        let text = "0.35元/天"
        label.text(text)
        label.font(9)
        label.textColor = .hexStringColor(hexString: "#818181")
//        if let range = text.range(of: "/") {
//            let nsRange = NSRange(text.startIndex..<range.lowerBound, in: text)
//            label.setRangeFontText(font: .textM(20), range: nsRange)
//        }
        return label
    }()
    
    lazy var priceLabel: UILabel = {
       
        let label = UILabel()
        label.textAlignment = .center
//        label.addRadius(radius: 12.rpx)
//        label.backgroundColor = .hexStringColor(hexString: "#F6F6F6")
        label.text("¥128")
        label.mediumFont(24)
        label.textColor = .hexStringColor(hexString: "#F12A1D")
        label.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#F12A1D"), font: UIFont.systemFont(ofSize: 14, weight: .medium))
        return label
    }()
    
    lazy var originPriceLabel: UILabel = {
        
        let label = UILabel()
        label.text("¥299")
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#A7A7A7")
        label.centerLineText(lineValue: 1, underlineColor: .hexStringColor(hexString: "#A7A7A7"))
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func config(model: QSLGoodModel) {
        self.goodModel = model
        
        if model.isSelect {
            self.bgView.layer.borderColor = UIColor.hexStringColor(hexString: "#E7B983").cgColor
            self.bgView.backgroundColor = .hexStringColor(hexString: "#FFF8EF")
        } else {
            self.bgView.layer.borderColor = UIColor.hexStringColor(hexString: "#EEEEEE").cgColor
            self.bgView.backgroundColor = .white
        }
        
        self.tagIcon.isHidden = !model.popular
        
        self.goodNameLabel.text = model.name
//        self.goodDailyPriceLabel.text = "0.001元/天"
        self.goodDailyPriceLabel.text = model.content
        let width = model.content.singleLineWidth(font: .textF(10)) + 24.rpx
        self.goodDailyPriceLabel.snp.updateConstraints { make in
            make.width.equalTo(width)
        }
//        if let range = model.content.range(of: "/") {
//            let nsRange = NSRange(model.content.startIndex..<range.lowerBound, in: model.content)
//            self.goodDailyPriceLabel.setRangeFontText(font: .textM(20), range: nsRange)
//        }

        var priceText = ""
        if model.amount.truncatingRemainder(dividingBy: 100) == 0 {
            priceText = "¥\(Int(model.amount / 100))"
        } else {
            priceText = String(format: "¥%.2lf", model.amount / 100 )
        }
        self.priceLabel.text = priceText
        self.priceLabel.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#F12A1D"), font: UIFont.systemFont(ofSize: 14, weight: .medium))
//        let width = priceText.singleLineWidth(font: .textF(12)) + 28.rpx
//        self.priceLabel.snp.updateConstraints { make in
//            make.width.equalTo(width)
//        }
        
        var orinalPriceText = ""
        if model.originalAmount.truncatingRemainder(dividingBy: 100) == 0 {
            orinalPriceText = "¥\(Int(model.originalAmount / 100))"
        } else {
            orinalPriceText = String(format: "¥%.2lf", model.originalAmount / 100 )
        }
        self.originPriceLabel.text = orinalPriceText
    }
}

extension QSLVipGoodCollectionViewCell {
    
    func initView() {
        
        self.contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.right.bottom.equalTo(0)
            make.left.equalTo(6.rpx)
            make.top.equalTo(11.rpx)
        }
        
        self.contentView.addSubview(tagIcon)
        tagIcon.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.size.equalTo(CGSize(width: 79.rpx, height: 29.rpx))
        }
        
        self.bgView.addSubview(goodNameLabel)
        goodNameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(16.rpx)
        }
        
        self.bgView.addSubview(priceLabel)
//        let width = "￥128".singleLineWidth(font: .textF(12)) + 28.rpx
        priceLabel.snp.makeConstraints { make in
//            make.width.equalTo(width)
//            make.height.equalTo(21.rpx)
            make.top.equalTo(goodNameLabel.snp.bottom).offset(5.rpx)
            make.top.equalTo(45.rpx)
            make.centerX.equalToSuperview()
        }
        
        self.bgView.addSubview(originPriceLabel)
        originPriceLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(priceLabel.snp.bottom).offset(5.rpx)
        }
        
        let width = "0.001元/天".singleLineWidth(font: .textF(10)) + 24.rpx
        self.bgView.addSubview(goodDailyPriceLabel)
        goodDailyPriceLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-8.rpx)
            make.height.equalTo(21.rpx)
            make.width.equalTo(width)
        }
        
    }
}
