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

import UIKit

enum GoodCellType {
    case big
    case small
}

class QSLVipGoodCollectionViewCell: UICollectionViewCell {
    
    struct UX {
        static let smallMainViewWidth = (QSLConst.qsl_kScreenW - 42.0.rpx) / 3.0 - 9.0.rpx
        static let mainViewHeight = 94.0.rpx
        static let bigMainViewWidth = (QSLConst.qsl_kScreenW - 36.0.rpx) / 2.0 - 9.0.rpx
    }
    
    var type: GoodCellType = .small {
        didSet {
            self.updateUI()
        }
    }
    
    var goodModel: QSLGoodModel?
    
    lazy var bgView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .hexStringColor(hexString: "#EEEEEE")
        view.addRadius(radius: 12)
        return view
    }()
    
    lazy var mostTagIcon: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "vip_goods_most_tag")
        return imageView
    }()
    
    lazy var tagIcon: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "vip_goods_tag_icon")
        return imageView
    }()
    
    lazy var mainView: UIView = {
       
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UX.smallMainViewWidth, height: UX.mainViewHeight))
        view.addFourCorner(topLeft: 11, topRight: 11, bottomLeft: 0, bottomRight: 0)
        view.backgroundColor = .white
        return view
    }()

    lazy var goodNameLabel: UILabel = {
        
        let label = UILabel()
        label.text("年度会员")
        label.boldFont(13)
        label.textColor = .hexStringColor(hexString: "#404040")
        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: "#404040")
        label.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#404040"), 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
    }()
    
    lazy var goodDailyPriceLabel: UILabel = {
        
        let label = UILabel()
        let text = "仅需¥9.2/天，时刻守护Ta的安全"
        label.text(text)
        label.font(9)
        label.textColor = .hexStringColor(hexString: "#818181")
        label.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#818181"), font: UIFont.systemFont(ofSize: 9, weight: .heavy))
        
        if let yuanIndex = text.firstIndex(of: "¥") {
            
            // 获取文本最后一位的index
            let lastIndex = text.index(before: text.endIndex)
            
            // 创建从¥后面一个字符到文本最后一位的range
            let contentRange = text.index(after: yuanIndex)..<lastIndex
            
            // 将Swift的Range转换为NSRange
            let nsRange = NSRange(
                contentRange,
                in: text
            )
            
            label.setRangeFontText(font: UIFont.systemFont(ofSize: 12, weight: .heavy), range: nsRange)
        }
        
        return label
    }()
    
    lazy var selectBtn: UIButton = {
      
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "vip_good_unselect_big"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "vip_good_select_big"), for: .selected)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func config(model: QSLGoodModel, type: GoodCellType) {
        
        self.goodModel = model
        self.type = type
        
        self.selectBtn.isSelected = model.isSelect
        let text = model.content
        self.goodDailyPriceLabel.text = text
        if model.isSelect {
            self.bgView.backgroundColor = .hexStringColor(hexString: "#15CBA1")
            
            self.goodDailyPriceLabel.textColor = .white
            self.goodDailyPriceLabel.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#FFFFFF"), font: UIFont.systemFont(ofSize: 9, weight: .heavy))
            
            if let yuanIndex = text.firstIndex(of: "¥") {
                
                // 获取文本最后一位的index
                let lastIndex = text.index(before: text.endIndex)
                
                // 创建从¥后面一个字符到文本最后一位的range
                let contentRange = text.index(after: yuanIndex)...lastIndex
                
                // 将Swift的Range转换为NSRange
                let nsRange = NSRange(
                    contentRange,
                    in: text
                )
                
                self.goodDailyPriceLabel.setRangeFontText(font: UIFont.systemFont(ofSize: 12, weight: .heavy), range: nsRange)
            }
            
        } else {
            self.bgView.backgroundColor = .hexStringColor(hexString: "#EEEEEE")
            self.goodDailyPriceLabel.textColor = .hexStringColor(hexString: "#818181")
            
            self.goodDailyPriceLabel.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#818181"), font: UIFont.systemFont(ofSize: 9, weight: .heavy))
            
            if let yuanIndex = text.firstIndex(of: "¥") {
                
                // 获取文本最后一位的index
                let lastIndex = text.index(before: text.endIndex)
                
                // 创建从¥后面一个字符到文本最后一位的range
                let contentRange = text.index(after: yuanIndex)...lastIndex
                
                // 将Swift的Range转换为NSRange
                let nsRange = NSRange(
                    contentRange,
                    in: text
                )
                
                self.goodDailyPriceLabel.setRangeFontText(font: UIFont.systemFont(ofSize: 12, weight: .heavy), range: nsRange)
            }
        }
        
        self.mostTagIcon.isHidden = !model.popular
        
        self.goodNameLabel.text = model.name
//        self.goodDailyPriceLabel.text = "0.001元/天"
//        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: "#404040"), 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 updateUI() {
        
        if self.type == .big {
            self.mainView.frame = CGRect(x: 0, y: 0, width: UX.bigMainViewWidth, height: UX.mainViewHeight)
            
            goodNameLabel.snp.remakeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(15.rpx)
            }
            
            priceLabel.snp.remakeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(40.rpx)
            }
            
            originPriceLabel.snp.remakeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(priceLabel.snp.bottom).offset(0.rpx)
            }
            
            goodDailyPriceLabel.snp.remakeConstraints { make in
                make.centerX.equalToSuperview()
                make.centerY.equalTo(selectBtn.snp.centerY)
            }
        } else {
            self.mainView.frame = CGRect(x: 0, y: 0, width: UX.smallMainViewWidth, height: UX.mainViewHeight)
        }
       
        self.mainView.addFourCorner(topLeft: 11, topRight: 11, bottomLeft: 0, bottomRight: 0)
    }
    
    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(mostTagIcon)
        mostTagIcon.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.size.equalTo(CGSize(width: 85.rpx, height: 29.rpx))
        }
        
//        self.addSubview(mostTagIcon)
//        mostTagIcon.snp.makeConstraints { make in
//            make.left.equalTo(0)
//            make.top.equalTo(0)
//            make.size.equalTo(CGSize(width: 85.rpx, height: 29.rpx))
//        }
        
        self.bgView.addSubview(mainView)
        mainView.snp.makeConstraints { make in
            make.top.equalTo(2.rpx)
            make.left.equalTo(2.rpx)
            make.right.equalTo(-2)
            make.height.equalTo(UX.mainViewHeight)
        }
        
        self.mainView.addSubview(goodNameLabel)
        goodNameLabel.snp.makeConstraints { make in
            make.left.equalTo(10.rpx)
            make.top.equalTo(15.rpx)
        }
        
        self.mainView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.top.equalTo(40.rpx)
            make.left.equalTo(10.rpx)
        }
        
        self.mainView.addSubview(originPriceLabel)
        originPriceLabel.snp.makeConstraints { make in
            make.left.equalTo(10.rpx)
            make.top.equalTo(priceLabel.snp.bottom).offset(0.rpx)
        }
        
        self.bgView.addSubview(selectBtn)
        selectBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 16.rpx, height: 16.rpx))
            make.right.equalTo(-6.rpx)
            make.bottom.equalTo(-6.rpx)
        }
        
        self.bgView.addSubview(goodDailyPriceLabel)
        goodDailyPriceLabel.snp.makeConstraints { make in
            make.left.equalTo(10.rpx)
            make.centerY.equalTo(selectBtn.snp.centerY)
        }
        

        
//        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)
//        }
        
    }
}
