//
//  YHInfoItemSelectSheetView.swift
//  galaxy
//
//  Created by edy on 2024/8/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHInfoItemSelectSheetView: UIView {
    
    var titleMaxWidth = 112.0
    
    var clickBlock: (()->())?
    
    var title: String = "" {
        didSet {
            titleLabel.text = title
        }
    }
    
    var placeHolder: String = ""
    
    var detail: String = "" {
        didSet {
            
            if detail.isEmpty {
                // 显示placeHolder
                detailLabel.text = self.placeHolder
                detailLabel.textColor = .placeHolderColor
                detailLabel.textAlignment = .right
                return
            }
            detailLabel.text = detail
            detailLabel.textColor = .mainTextColor
            detailLabel.textAlignment = .left
        }
    }
    
    var isShowDetailOneline:Bool = false {
        didSet {
//            detailLabel.textAlignment = isShowDetailOneline ? .right : .left
            detailLabel.numberOfLines = isShowDetailOneline ? 1 : 0
            detailLabel.snp.remakeConstraints { make in
                make.top.equalTo(16)
                make.left.equalTo(titleLabel.snp.right)
                make.right.equalTo(arrowImgView.snp.left)
                make.bottom.equalTo(-16)
                if isShowDetailOneline {
                    make.height.equalTo(20)
                }
            }
            self.setNeedsLayout()
            self.layoutIfNeeded()
        }
    }
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .separatorColor
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 14)
        label.textColor = .mainTextColor
        return label
    }()
    
    private lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.PFSC_M(ofSize: 14)
        label.textColor = .mainTextColor
        return label
    }()
    
    lazy var arrowImgView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "family_info_arrow"))
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     
    @objc func didSelectView() {
        clickBlock?()
    }
    
    func createUI() {
        self.addSubview(titleLabel)
        self.addSubview(detailLabel)
        self.addSubview(arrowImgView)
        self.addSubview(lineView)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(didSelectView))
        self.addGestureRecognizer(tap)

        lineView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.height.equalTo(0.5)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(16)
            make.width.equalTo(titleMaxWidth)
        }
        
        detailLabel.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(titleLabel.snp.right)
            make.right.equalTo(arrowImgView.snp.left)
            make.bottom.equalTo(-16)
        }
        
        arrowImgView.snp.makeConstraints { make in
            make.width.height.equalTo(22)
            make.top.equalTo(15)
            make.right.equalTo(0)
        }
    }

}
