//
//  YHPreviewInfoQuestionAndAnswerView.swift
//  galaxy
//
//  Created by davidhuangA on 2024/2/24.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHPreviewInfoQuestionAndAnswerItemView2: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    // title
    private lazy var nameLable: UILabel = {
        let lable0 = UILabel(text: "--")
        lable0.textColor = UIColor.labelTextColor2
        lable0.font = UIFont.PFSC_R(ofSize: 14)
        lable0.numberOfLines = 0
        lable0.lineBreakMode = .byCharWrapping
        return lable0
    }()
    
    // subtitleLable
    private lazy var subtitleLable: UILabel = {
        let lable0 = UILabel(text: "--")
        lable0.textColor = UIColor.mainTextColor
        lable0.font = UIFont.PFSC_R(ofSize: 14)
        lable0.numberOfLines = 0
        lable0.lineBreakMode = .byWordWrapping
        return lable0
    }()
    
    private lazy var answerkHoldView: UIView =  {
        let view = UIView()
        view.backgroundColor = .contentBkgColor
        view.layer.cornerRadius = kCornerRadius3
        view.clipsToBounds = true
        return view
    }()
    
    // remarkLable
    private lazy var remarkLable: UILabel = {
        let lable0 = UILabel(text: "--")
        lable0.textColor = UIColor.labelTextColor2
        lable0.font = UIFont.PFSC_R(ofSize: 14)
        lable0.numberOfLines = 0
        lable0.lineBreakMode = .byWordWrapping
        lable0.text = "--"
        return lable0
    }()
    
    private lazy var remarkHoldView: UIView =  {
        let view = UIView()
        view.backgroundColor = .contentBkgColor
        view.layer.cornerRadius = kCornerRadius3
        view.clipsToBounds = true
        view.isHidden = true
        return view
    }()
    
    private func setupUI() {
        addSubview(nameLable)
        nameLable.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(18)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        addSubview(answerkHoldView)
        
        answerkHoldView.addSubview(subtitleLable)
        subtitleLable.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(kMargin)
            make.right.bottom.equalToSuperview().offset(-kMargin)
        }
    }
    
    var dataModel: YHPreviewQuestionAndAnswerModel? {
        didSet {
            updateUI()
        }
    }
}

extension YHPreviewInfoQuestionAndAnswerItemView2 {
    func updateUI() {
        guard let model = dataModel else { return }
        
        removeSubviews()
        setupUI()
        
        nameLable.text = model.question
        subtitleLable.text = model.answer
        
        if let remark = model.remark, !remark.isEmpty {
            answerkHoldView.snp.removeConstraints()
            answerkHoldView.snp.remakeConstraints { make in
                make.top.equalTo(nameLable.snp.bottom).offset(7)
                make.left.equalToSuperview()
                make.right.equalToSuperview()
            }
            
            addSubview(remarkHoldView)
            remarkHoldView.snp.makeConstraints { make in
                make.top.equalTo(answerkHoldView.snp.bottom).offset(8)
                make.right.equalToSuperview()
                make.left.equalToSuperview()
            }
            remarkHoldView.addSubview(remarkLable)
            remarkLable.snp.makeConstraints { make in
                make.top.left.equalTo(12)
                make.bottom.right.equalTo(-12)
            }
            
            remarkLable.text = remark
            remarkHoldView.isHidden = false
            remarkHoldView.snp.makeConstraints { make in
                make.top.equalTo(answerkHoldView.snp.bottom).offset(8)
                make.right.equalToSuperview()
                make.left.equalToSuperview()
                make.bottom.equalToSuperview()
            }
        } else {
            
            answerkHoldView.snp.removeConstraints()
            subtitleLable.snp.removeConstraints()
            
            subtitleLable.snp.remakeConstraints { make in
                make.top.left.equalToSuperview().offset(kMargin)
                make.right.bottom.equalToSuperview().offset(-kMargin)
            }
            
            answerkHoldView.snp.remakeConstraints { make in
                make.top.equalTo(nameLable.snp.bottom).offset(7)
                make.left.equalToSuperview()
                make.right.equalToSuperview()
                
                make.bottom.equalToSuperview()
            }
        }
    }
}
