//
//  QSLActivityVipCell.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/9/23.
//

import UIKit

class QSLActivityVipCell: UICollectionViewCell {
    
    struct UX {
        static let topMainViewWidth = (QSLConst.qsl_kScreenW - 32.0.rpx)
        static let topMainViewHeight = 112.0.rpx
        static let smallMainViewWidth = (QSLConst.qsl_kScreenW-50.rpx)/2.0
        static let smallMainViewHeight = 112.0.rpx
        static let bigMainViewWidth = (QSLConst.qsl_kScreenW - 32.0.rpx)
        static let bigMainViewHeight = 72.0.rpx
    }
    
    var type: GoodCellType = .small {
        didSet {
            self.updateUI()
        }
    }
    
    var goodModel: QSLGoodModel?
    
    lazy var bgView: UIImageView = {
       
        let view = UIImageView()
        return view
    }()
    
    lazy var mainView: UIView = {
       
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UX.smallMainViewWidth, height: UX.smallMainViewHeight))
        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(16)
        label.textColor = .hexStringColor(hexString: "#333333")
        return label
    }()
    
    lazy var priceLabel: UILabel = {
       
        let label = UILabel()
        label.textAlignment = .center
        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.textAlignment = .center
        label.textColor = .hexStringColor(hexString: "#96BEBA")
        label.centerLineText(lineValue: 1, underlineColor: .hexStringColor(hexString: "#96BEBA"))
        return label
    }()
    
    lazy var goodDailyPriceLabel: UILabel = {
        
        let label = UILabel()
        let text = "仅需¥9.2/天，时刻守护Ta的安全"
        label.text(text)
        label.font(9)
        label.textColor = .hexStringColor(hexString: "#5EA69D")
        label.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#5EA69D"), 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

        var text = model.content
        self.goodDailyPriceLabel.text = text
        self.goodNameLabel.text = model.name
        
        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))

        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
        
        if(type == .top){
            self.bgView.layer.cornerRadius = 0
            self.bgView.layer.masksToBounds = true
            self.bgView.layer.borderWidth = 0
            if model.isSelect {
                self.bgView.image = UIImage(named: "vip_activity_item_s")
            }else{
                self.bgView.image = UIImage(named: "vip_activity_item_uns")
            }
            self.selectBtn.isHidden = true
            self.priceLabel.textColor = .red
            self.goodDailyPriceLabel.textColor = UIColor.hexStringColor(hexString: "#5EA69D")
            self.originPriceLabel.textColor = UIColor.hexStringColor(hexString: "#96BEBA")
            
            return
        }
        
        self.priceLabel.textColor = .hexStringColor(hexString: "#333333")
        self.selectBtn.isHidden = true
        self.selectBtn.isSelected = model.isSelect
        self.bgView.image = UIImage.init()
        
        self.bgView.layer.cornerRadius = 14.rpx
        self.bgView.layer.masksToBounds = true
        self.bgView.layer.borderWidth = 2
        self.goodDailyPriceLabel.textColor = UIColor.init(white: 0, alpha: 0.4)
        self.originPriceLabel.textColor = UIColor.init(white: 0, alpha: 0.4)
        
        if model.isSelect {
            self.bgView.layer.borderColor = UIColor.hexStringColor(hexString: "#003D46").cgColor
            
            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.layer.borderColor = UIColor.hexStringColor(hexString: "#DDDDDD").cgColor
            
            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)
            }
        }

    }
}

extension QSLActivityVipCell {
    
    func updateUI() {
        self.goodNameLabel.textAlignment = .left
        self.priceLabel.textAlignment = .left
        self.originPriceLabel.textAlignment = .left
        self.goodDailyPriceLabel.textAlignment = .left
        if self.type == .top {
            self.mainView.backgroundColor = UIColor.init(white: 0, alpha: 0)
            self.mainView.frame = CGRect(x: 0, y: 0, width: UX.topMainViewWidth, height: UX.topMainViewHeight)
            
            goodNameLabel.snp.remakeConstraints { make in
                make.left.equalTo(89.rpx)
                make.top.equalTo(48.rpx)
                make.width.equalTo(100.rpx)
                make.height.equalTo(22.rpx)
            }
            
            priceLabel.snp.remakeConstraints { make in
                make.left.equalTo(16.rpx)
                make.top.equalTo(48.rpx)
                make.width.equalTo(80.rpx)
                make.height.equalTo(22.rpx)
            }
            
            originPriceLabel.snp.remakeConstraints { make in
                make.width.equalTo(80.rpx)
                make.height.equalTo(22.rpx)
                make.left.equalTo(16.rpx)
                make.top.equalTo(priceLabel.snp.bottom).offset(0.rpx)
            }
            
            goodDailyPriceLabel.snp.remakeConstraints { make in
                make.left.equalTo(89.rpx)
                make.top.equalTo(77.rpx)
                make.right.equalTo(-10.rpx)
                make.height.equalTo(12.rpx)
            }
            
        } else if self.type == .big {
            
            self.mainView.backgroundColor = UIColor.white
            self.mainView.frame = CGRect(x: 0, y: 0, width: UX.bigMainViewWidth, height: UX.bigMainViewHeight)
            
            goodNameLabel.snp.remakeConstraints { make in
                make.left.equalTo(89.rpx)
                make.top.equalTo(15.rpx)
                make.width.equalTo(100.rpx)
                make.height.equalTo(22.rpx)
            }
            
            priceLabel.snp.remakeConstraints { make in
                make.left.equalTo(16.rpx)
                make.top.equalTo(15.rpx)
                make.width.equalTo(80.rpx)
                make.height.equalTo(22.rpx)
            }
            
            originPriceLabel.snp.remakeConstraints { make in
                make.width.equalTo(80.rpx)
                make.height.equalTo(22.rpx)
                make.left.equalTo(16.rpx)
                make.top.equalTo(priceLabel.snp.bottom).offset(0.rpx)
            }
            
            goodDailyPriceLabel.snp.remakeConstraints { make in
                make.left.equalTo(89.rpx)
                make.top.equalTo(goodNameLabel.snp.bottom).offset(2.rpx)
                make.right.equalTo(-10.rpx)
                make.height.equalTo(12.rpx)
            }
        } else {
            self.goodNameLabel.textAlignment = .center
            self.priceLabel.textAlignment = .center
            self.originPriceLabel.textAlignment = .center
            self.goodDailyPriceLabel.textAlignment = .center
            self.mainView.backgroundColor = UIColor.white
            self.mainView.frame = CGRect(x: 0, y: 0, width: UX.smallMainViewWidth, height: UX.smallMainViewHeight)
            
            goodNameLabel.snp.remakeConstraints { make in
                make.left.equalTo(10.rpx)
                make.right.equalTo(-10.rpx)
                make.top.equalTo(10.rpx)
                make.height.equalTo(14.rpx)
            }
            
            priceLabel.snp.remakeConstraints { make in
                make.left.equalTo(10.rpx)
                make.right.equalTo(-10.rpx)
                make.top.equalTo(goodNameLabel.snp.bottom).offset(10.rpx)
                make.height.equalTo(30.rpx)
            }
            
            originPriceLabel.snp.remakeConstraints { make in
                make.left.equalTo(10.rpx)
                make.right.equalTo(-10.rpx)
                make.top.equalTo(priceLabel.snp.bottom).offset(0)
                make.height.equalTo(15.rpx)
            }
            
            goodDailyPriceLabel.snp.remakeConstraints { make in
                make.left.equalTo(10.rpx)
                make.right.equalTo(-10.rpx)
                make.top.equalTo(originPriceLabel.snp.bottom).offset(8.rpx)
                make.height.equalTo(15.rpx)
            }
        }
       
        self.mainView.addFourCorner(topLeft: 11, topRight: 11, bottomLeft: 0, bottomRight: 0)
    }
    
    func initView() {
        
        self.contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
        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(72.rpx)
        }
        
        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)
        }
        
    }
}
