//
//  VIPPrivilegesViews.swift

//
//  Created by Leon on 2025/9/16.
//

class VIPPrivilegesListCell: UITableViewCell {
    var block: UIView!
    var gradientBorderContainer: GradientBorderView!
    
    var icon: UIImageView!
    var contentStackV: UIStackView!
    var titleLabel: LineSpaceLabel!
    var descLabel: LineSpaceLabel!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        selectionStyle = .none
        backgroundColor = .clear

        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        block = {
            let v = UIView()
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.trailing.bottom.equalToSuperview()
                make.top.equalToSuperview().offset(16)
            }
            return v
        }()
        
        gradientBorderContainer = {
            let gradient = CLSystemToken.gradient(token: .ccvn)
            
            let v = GradientBorderView(colors: gradient.colors(), gradientType: .leftToRight)
            v.gBorderWidth = 1
            v.layer.cornerRadius = 8
            v.layer.masksToBounds = true
            v.backgroundColor = .c.cbdi
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.size.equalTo(CGSize(width: 160, height: 107))
                make.top.greaterThanOrEqualToSuperview().offset(0)
                make.bottom.lessThanOrEqualToSuperview()
            }
            return v
        }()
        
        icon = {
            let v = UIImageView()
            v.cornerRadius = 8
//            block.addSubview(v)
//            v.snp.makeConstraints { make in
//                make.leading.equalToSuperview().offset(24)
//                make.size.equalTo(CGSize(width: 160, height: 107))
//                make.top.greaterThanOrEqualToSuperview().offset(0)
//                make.bottom.lessThanOrEqualToSuperview()
//            }
            gradientBorderContainer.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()

        contentStackV = {
            let v = UIStackView()
            v.axis = .vertical
            v.spacing = 4
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalTo(gradientBorderContainer.snp.trailing).offset(16)
                make.trailing.equalToSuperview().offset(-24)
                make.top.greaterThanOrEqualToSuperview()
                make.bottom.lessThanOrEqualToSuperview()
                make.centerY.equalToSuperview()
            }
            return v
        }()

        titleLabel = {
            let v = LineSpaceLabel()
            let typo = CLSystemToken.typography(token: .tts)
            v.config(typo)
            v.textColor = .text
            contentStackV.addArrangedSubview(v)
            return v
        }()

        descLabel = {
            let v = LineSpaceLabel()
            let typo = CLSystemToken.typography(token: .tbs)
            v.config(typo)
            v.textColor = .c.ctsn
            contentStackV.addArrangedSubview(v)
            return v
        }()

//        testData()
    }

    private func testData() {
        icon.backgroundColor = .random
        titleLabel.text = "Virtual character X5"
        descLabel.text = "Add 4 virtual role creation opportunities"
    }
    
    func config(_ data: MemberPrivDict?){
        guard let privilege = data else {return}
        icon.loadImage(privilege.img, bgColor: .clear)
        titleLabel.text = privilege.title
        descLabel.text = privilege.desc
    }
}

class VIPPrivilegesHeadView: UIView {
    var gradientBg: GradientView!
    var titleLabel: CLLabel!
    var descLabel: LineSpaceLabel!

    var vipIcon: UIImageView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        gradientBg = {
            let gradient = CLSystemToken.gradient(token: .ccvn)
            let v = GradientView(colors: gradient.colors(), gradientType: .leftToRight)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()

        vipIcon = {
            let v = UIImageView()
            v.image = UIImage(named: "icon_vip_crown")
            addSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 100, height: 100))
                make.trailing.equalToSuperview().offset(-24)
                make.bottom.equalToSuperview().offset(-24)
            }
            return v
        }()

        titleLabel = {
            let v = CLLabel()
            v.font = .t.tdl
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.top.equalToSuperview().offset(UIWindow.navBarTotalHeight)
            }
            return v
        }()

        descLabel = {
            let v = LineSpaceLabel()
            let typo = CLSystemToken.typography(token: .tls)
            v.config(typo)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(24)
                make.top.equalTo(titleLabel.snp.bottom).offset(4)
                make.trailing.equalTo(vipIcon.snp.leading).offset(-16)
            }
            return v
        }()

        titleLabel.text = "Crushlevel VIP"
        descLabel.text = "Only $5.99/month, enjoy more benefits"
    }
    
    func config(info: UserSubscription?){
        guard let vip = info else {return}
        
        if vip.getVIPValid(), let expTime = vip.expTime{
            let dateString = Date.timerStyle(style: .DMYHM, millisecond: expTime)
            descLabel.text = "VIP expired in \(dateString)"
        }
    }
}
