//
//  QSLVipMostGoodCell.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/5/28.
//

import UIKit

class QSLVipMostGoodCell: UIView {
    
    struct UX {
        static let mainViewHeight = 71.0.rpx
        static let mainViewWidth = QSLConst.qsl_kScreenW - 36.0.rpx
    }
    
    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 mainView: UIView = {
       
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UX.mainViewWidth, height: UX.mainViewHeight))
        view.addFourCorner(topLeft: 11, topRight: 11, bottomLeft: 0, bottomRight: 0)
        view.backgroundColor = .white
        return view
    }()
    
    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
    }()
    
    lazy var titleLabel: UILabel = {
       
        let label = UILabel()
        label.text("周度会员")
        label.boldFont(14)
        label.textColor = .hexStringColor(hexString: "#404040")
        return label
    }()
    
    lazy var describeLabel: UILabel = {
       
        let label = UILabel()
        label.text("96%用户首选")
        label.boldFont(9)
        label.textColor = .hexStringColor(hexString: "#11BD96")
        return label
    }()
    
    lazy var processBgView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .hexStringColor(hexString: "#BAFFF1")
        view.addRadius(radius: 2.5.rpx)
        return view
    }()
    
    lazy var processView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .hexStringColor(hexString: "#15CBA1")
        view.addRadius(radius: 2.5.rpx)
        return view
    }()

    lazy var recommendIcon: UIImageView = {
       
        let icon = UIImageView()
        icon.image = UIImage(named: "vip_recommend_tag")
        return icon
    }()
    
    lazy var dailyLabel: UILabel = {
      
        let label = UILabel()
        let text = "仅需¥9.2/天，时刻守护Ta的安全"
        label.text(text)
        label.font(11)
        label.textColor = .hexStringColor(hexString: "#818181")
        label.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#818181"), font: UIFont.systemFont(ofSize: 11, weight: .heavy))
        
        if let yuanIndex = text.firstIndex(of: "¥"),
           let slashIndex = text.firstIndex(of: "/") {
            
            // 创建从¥后面一个字符到/前面一个字符的range
            let contentRange = text.index(after: yuanIndex)..<slashIndex
            
            // 将Swift的Range转换为NSRange
            let nsRange = NSRange(
                contentRange,
                in: text
            )
            
            label.setRangeFontText(font: UIFont.systemFont(ofSize: 16, 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)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func config(model: QSLGoodModel) {
        self.goodModel = model
        
        self.selectBtn.isSelected = model.isSelect
        let text = model.content
        self.dailyLabel.text = text
        if model.isSelect {
            self.bgView.backgroundColor = .hexStringColor(hexString: "#15CBA1")
            self.mainView.gradientBackgroundColor(color1: .hexStringColor(hexString: "#CAFFF3"), color2: .hexStringColor(hexString: "#FFFFFF"), width: UX.mainViewWidth, height: UX.mainViewHeight, direction: .horizontal)
            
            self.dailyLabel.textColor = .white
            self.dailyLabel.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#FFFFFF"), font: UIFont.systemFont(ofSize: 9, weight: .heavy))
            
            if let yuanIndex = text.firstIndex(of: "¥"),
               let slashIndex = text.firstIndex(of: "/") {
                
                // 创建从¥后面一个字符到/前面一个字符的range
                let contentRange = text.index(after: yuanIndex)..<slashIndex
                
                // 将Swift的Range转换为NSRange
                let nsRange = NSRange(
                    contentRange,
                    in: text
                )
                
                self.dailyLabel.setRangeFontText(font: UIFont.systemFont(ofSize: 16, weight: .heavy), range: nsRange)
            }
            
        } else {
            self.bgView.backgroundColor = .hexStringColor(hexString: "#EEEEEE")
            self.mainView.backgroundColor = .white
            
            self.dailyLabel.textColor = .hexStringColor(hexString: "#818181")
            
            self.dailyLabel.setSpecificTextColorFont("¥", color: .hexStringColor(hexString: "#818181"), font: UIFont.systemFont(ofSize: 9, weight: .heavy))
            
            if let yuanIndex = text.firstIndex(of: "¥"),
               let slashIndex = text.firstIndex(of: "/") {
                
                // 创建从¥后面一个字符到/前面一个字符的range
                let contentRange = text.index(after: yuanIndex)..<slashIndex
                
                // 将Swift的Range转换为NSRange
                let nsRange = NSRange(
                    contentRange,
                    in: text
                )
                
                self.dailyLabel.setRangeFontText(font: UIFont.systemFont(ofSize: 16, weight: .heavy), range: nsRange)
            }
        }
        
        self.mostTagIcon.isHidden = !model.popular
        
        self.titleLabel.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: "#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 QSLVipMostGoodCell {
    
    func setUI() {
        
        self.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.right.bottom.equalTo(0)
            make.left.equalTo(6.rpx)
            make.top.equalTo(11.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.left.top.equalTo(2.rpx)
            make.right.equalTo(-2.rpx)
            make.height.equalTo(71.rpx)
        }
        
        self.mainView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(17.rpx)
            make.top.equalTo(17.rpx)
        }
        
        self.mainView.addSubview(originPriceLabel)
        originPriceLabel.snp.makeConstraints { make in
            make.centerX.equalTo(priceLabel.snp.centerX)
            make.top.equalTo(priceLabel.snp.bottom)
        }
        
        self.mainView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(17.rpx)
            make.left.equalTo(84.rpx)
        }
        
        self.mainView.addSubview(processBgView)
        processBgView.snp.makeConstraints { make in
            make.bottom.equalTo(-13.rpx)
            make.left.equalTo(titleLabel.snp.left)
            make.width.equalTo(138.rpx)
            make.height.equalTo(5.rpx)
        }
        
        self.processBgView.addSubview(processView)
        processView.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(0)
            make.right.equalTo(-20.rpx)
        }
        
        self.mainView.addSubview(describeLabel)
        describeLabel.snp.makeConstraints { make in
            make.left.equalTo(processBgView.snp.left)
            make.bottom.equalTo(processBgView.snp.top).offset(-2.rpx)
        }
        
        self.mainView.addSubview(recommendIcon)
        recommendIcon.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 73.rpx, height: 71.rpx))
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        self.bgView.addSubview(dailyLabel)
        dailyLabel.snp.makeConstraints { make in
            make.left.equalTo(20.rpx)
            make.bottom.equalTo(-7.rpx)
        }
        
        self.bgView.addSubview(selectBtn)
        selectBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 18.rpx, height: 18.rpx))
            make.right.equalTo(-7.rpx)
            make.bottom.equalTo(-7.rpx)
        }
    }
}
