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

import UIKit

enum YHOtherSelecteItemType: Int {
    case unknow = 0
    case language = 1 //语言能力
    case skill = 2 //专业技能
}


class YHOtherSelecteItemView: UIView {

    static let viewH : CGFloat = 52 // type == 1时的高度
    static let viewH2 : CGFloat = 88 //type == 2时的高度
    
    //1- 一行布局  2-2行布局
    var type : Int = 1 {
        didSet {
            layoutIfNeeded()
        }
    }
    
    //获取高度
    static func getViewHBy(type : Int)->CGFloat {
        return type == 1 ? viewH : viewH2
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .mainTextColor
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 14)
        label.lineBreakMode = .byCharWrapping
        return label
    }()
    
    private lazy var textField: UITextField = {
        let label = UITextField()
        label.placeholder = "请选择"
        label.isEnabled = false
        label.font = UIFont.PFSC_R(ofSize: 14)
        return label
    }()
    
    
    private lazy var bottomLine : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: 0xF0F0F0)
        return view
    }()
    
    var title:String? {
        didSet {
            if let question = title {
                let str = "* "+question
                let attributes: [NSAttributedString.Key: Any] = [
                    .font: UIFont.PFSC_R(ofSize: 14),
                    .foregroundColor: UIColor.mainTextColor
                ]
                let questionAttrStr = NSMutableAttributedString(string: str, attributes: attributes)
                let starRange = NSRange(location: 0, length: 1)
                questionAttrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.failColor, range: starRange)
                titleLabel.attributedText = questionAttrStr
            }
        }
    }
    
    var subHoldView : UIView = {
        let view = UIView()
        return view
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if type == 1 {
            bottomLine.snp.makeConstraints { make in
                make.bottom.left.right.equalToSuperview()
                make.height.equalTo(0.5)
            }
            
            titleLabel.snp.makeConstraints { make in
                make.top.left.bottom.equalToSuperview()
                make.width.equalTo(80)
            }
            
            subHoldView.snp.makeConstraints { make in
                make.top.right.bottom.equalToSuperview()
                make.left.equalTo(titleLabel.snp.right).offset(40)
            }
        } else if type == 2 {
            bottomLine.snp.makeConstraints { make in
                make.bottom.left.right.equalToSuperview()
                make.height.equalTo(0.5)
            }
            
            subHoldView.snp.makeConstraints { make in
                make.bottom.equalTo(bottomLine.snp.top)
                make.right.left.equalToSuperview()
                make.height.equalTo(52)
            }
            
            
            titleLabel.snp.makeConstraints { make in
                make.bottom.equalTo(subHoldView.snp.top)
                make.left.right.equalToSuperview()
                make.height.equalTo(20)
            }
        } else {
            
        }
    }
}


extension YHOtherSelecteItemView {
    func setupUI() {
        addSubview(bottomLine)
        addSubview(titleLabel)
        addSubview(subHoldView)
        
        title = "开始时间"
        
        
        let rightIcon = UIImageView()
        rightIcon.image = UIImage(named: "form_right_arrow")
        rightIcon.contentMode = .scaleAspectFill
        
        subHoldView.addSubview(rightIcon)
        rightIcon.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
            make.width.height.equalTo(20)
        }
        subHoldView.addSubview(textField)
        textField.snp.makeConstraints { make in
            make.right.equalTo(rightIcon.snp.left).offset(-4)
            make.bottom.top.left.equalToSuperview()
        }
    }
}

extension YHOtherSelecteItemView {
    func updateKeyName(name : String, keyValue : String) {
        title = name
        textField.text = keyValue
    }
}
