//
//  YHItemView.swift
//  galaxy
//
//  Created by EDY on 2024/1/25.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHItemView: UIView {
    var titleLabel: UILabel!
    var messageTextField: UITextField!
    var nextStepImageView: UIImageView!
    var centerButton: UIButton!
    var leftButton: UIButton!
    var rightButton: UIButton!
    
    var dataSource: YHItemModel? {
        didSet {
            updateAllViews()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        setUpView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpView() {
        titleLabel = {
            let label = UILabel()
            label.numberOfLines = 0
            return label
        }()
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(112)
        }
        
        messageTextField = {
            let textField = UITextField()
            return textField
        }()
        addSubview(messageTextField)
        messageTextField.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.left.equalTo(112)
        }
        
        nextStepImageView = {
            let imageView = UIImageView()
            imageView.image = UIImage(named: "form_right_arrow")
            return imageView
        }()
        addSubview(nextStepImageView)
        nextStepImageView.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.width.equalTo(20)
        }
        
        centerButton = {
            let button = UIButton(type: .custom)
            return button
        }()
        addSubview(centerButton)
        centerButton.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.left.equalTo(112)
        }
        
        rightButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor = UIColor(hex: 0x4487f9).withAlphaComponent(0.08)
            button.setTitleColor(UIColor(hex: 0x4487f9), for: .selected)
            button.setTitleColor(UIColor(hex: 0x222222), for: .normal)
            button.layer.cornerRadius = 16
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor(hex: 0x4487f9).cgColor
            button.isSelected = true
            return button
        }()
        addSubview(rightButton)
        rightButton.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(32)
            make.width.equalTo(74)
        }
        
        leftButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor = UIColor(hex: 0xf8f9fb)
            button.setTitleColor(UIColor(hex: 0x4487f9), for: .selected)
            button.setTitleColor(UIColor(hex: 0x222222), for: .normal)
            button.layer.cornerRadius = 16
            return button
        }()
        addSubview(leftButton)
        leftButton.snp.makeConstraints { make in
            make.right.equalTo(rightButton.snp.left).offset(-12)
            make.centerY.equalToSuperview()
            make.height.equalTo(32)
            make.width.equalTo(74)
        }
        
    }
    
    func updateAllViews() {
        guard let dataSource = dataSource else { return }
        if dataSource.isNeed ?? false {
            let str = "*" + (dataSource.title ?? "")
            let attributes: [NSAttributedString.Key: Any] = [
                .font: kFont(size: 14),
                .foregroundColor: UIColor(hex:0x222222)
            ]
            let questionAttrStr = NSMutableAttributedString(string: str, attributes: attributes)
            let starRange = NSRange(location: 0, length: 1)
            questionAttrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(hex:0xFF3A3A), range: starRange)
            titleLabel.attributedText = questionAttrStr
        } else {
            let str = dataSource.title ?? ""
            let attributes: [NSAttributedString.Key: Any] = [
                .font: kFont(size: 14),
                .foregroundColor: UIColor(hex:0x222222)
            ]
            let questionAttrStr = NSMutableAttributedString(string: str, attributes: attributes)
            titleLabel.attributedText = questionAttrStr
        }
        
        if dataSource.isUserKeyBoard ?? false {
            nextStepImageView.isHidden = true
            centerButton.isHidden = true
        } else {
            nextStepImageView.isHidden = false
            centerButton.isHidden = false
        }
        messageTextField.placeholder = dataSource.prompts
        messageTextField.text = dataSource.message
        if (dataSource.leftButtonString != nil) {
            leftButton.isHidden = false
            leftButton.setTitle(dataSource.leftButtonString, for: .normal)
            nextStepImageView.isHidden = true
        } else {
            leftButton.isHidden = true
        }
        
        if (dataSource.rightButtonString != nil) {
            rightButton.isHidden = false
            rightButton.setTitle(dataSource.rightButtonString, for: .normal)
            nextStepImageView.isHidden = true
        } else {
            rightButton.isHidden = true
        }
    }
}
