//
//  YHFormItemExpireDateCell.swift
//  galaxy
//
//  Created by edy on 2024/1/27.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHFormItemExpireDateCell: UITableViewCell {

    static let cellReuseIdentifier = "YHFormItemExpireDateCell"
    let longTimeBtnWidth = 40.0
    // 是否必填 如必填title会展示红色*
    var isMust = false
    
    var clickBlock:(()->Void)?

    var title:String? {
        didSet {
            
            if let title = title {
                let str = (isMust ? ("*"+title) : title)
                let attributes: [NSAttributedString.Key: Any] = [
                    .font: UIFont.PFSC_R(ofSize: 14),
                    .foregroundColor: UIColor(hex:0x222222)]
                let questionAttrStr = NSMutableAttributedString(string: str, attributes: attributes)
                if isMust {
                    let starRange = NSRange(location: 0, length: 1)
                    questionAttrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(hex:0xFF3A3A), range: starRange)
                }
                titleLabel.attributedText = questionAttrStr
            }
        }
    }
    
    var isLontTime:Bool = false {
        didSet {
            updateUI()
        }
    }
    
    var tips:String? {
        didSet {
            tipsLabel.text = tips
        }
    }
    
    var detail:String? {
        didSet {
            detailLabel.text = detail
            
            if let detail = detail, !detail.isEmpty {
                tipsLabel.isHidden = true
                detailLabel.isHidden = false
            } else {
                tipsLabel.isHidden = false
                detailLabel.isHidden = true
            }
        }
    }
    

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString:"#333333")
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 14)
        return label
    }()
    
    private lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#222222")
        label.textAlignment = .left
        label.font = UIFont.PFSC_R(ofSize: 14)
        label.isHidden = true
        return label
    }()
    
    private lazy var tipsLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#C0C0C0")
        label.text = "请选择".local
        label.textAlignment = .left
        label.font = UIFont.PFSC_R(ofSize: 14)
        label.isHidden = false
        return label
    }()
    
    lazy var longTimeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("长期".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_R(ofSize: 14)
        btn.setTitleColor(UIColor(hex: 0x222222), for: .normal)
        btn.iconInLeft(spacing: 6)
        btn.setImage(UIImage(named: "form_square_unselect"), for: .normal)
        btn.setImage(UIImage(named: "form_square_select"), for: .selected)
        btn.addTarget(self, action: #selector(didClickLongTimeBtn), for: .touchUpInside)
        return btn
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI() {
        
        self.selectionStyle = .none
        contentView.addSubview(titleLabel)
        contentView.addSubview(detailLabel)
        contentView.addSubview(tipsLabel)
        contentView.addSubview(longTimeBtn)

        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(detailLabel.snp.left).offset(-6)
            make.width.equalTo(120)
            make.centerY.equalToSuperview()
        }
        
        detailLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-longTimeBtnWidth-16-6)
            make.centerY.equalToSuperview()
        }
        
        tipsLabel.snp.makeConstraints { make in
            make.edges.equalTo(detailLabel)
        }
        longTimeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(longTimeBtnWidth, 40))
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
    }
    
    func updateUI() {
    
        detailLabel.isHidden = isLontTime
        tipsLabel.isHidden = isLontTime
        
        longTimeBtn.snp.remakeConstraints { make in
            make.size.equalTo(CGSizeMake(longTimeBtnWidth, 40))
            make.centerY.equalToSuperview()
            if isLontTime {
                make.left.equalTo(detailLabel)
            } else {
                make.right.equalToSuperview().offset(-16)
            }
        }
    }
    
    @objc func didClickLongTimeBtn() {
     
        longTimeBtn.isSelected = !longTimeBtn.isSelected
        isLontTime = longTimeBtn.isSelected
    
        if let clickBlock = clickBlock {
            clickBlock()
        }
    }
}
