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

import UIKit

class YHBasicInfoFillView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    typealias Block = () -> ()
    var block: Block?
    
    
    
    private let btnWidth = 70.0
    private let btnHeight = 32.0
    private let btnTitleSelectColor = UIColor.brandMainColor
    private let btnTitleDefaultColor = UIColor.mainTextColor
    private let btnBgDefaultColor = UIColor.contentBkgColor
    private let btnBgSelectColor = UIColor.brandMainColor.withAlphaComponent(0.08)
    private let answerBaseTag  = 9527
    
    
    
    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 answer1Btn: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("否", for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_R(ofSize: 14)
        btn.setTitleColor(btnTitleDefaultColor, for: .normal)
        btn.backgroundColor = btnBgDefaultColor
        btn.layer.cornerRadius = btnHeight/2.0
        btn.layer.masksToBounds = true
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.clear.cgColor
        btn.tag = answerBaseTag
        btn.addTarget(self, action: #selector(didClickResponseBtn(btn:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var answer2Btn: UIButton = {
        let btn = UIButton()
        btn.setTitle("是", for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_R(ofSize: 14)
        btn.setTitleColor(btnTitleDefaultColor, for: .normal)
        btn.backgroundColor = btnBgDefaultColor
        btn.layer.cornerRadius = btnHeight/2.0
        btn.layer.masksToBounds = true
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.clear.cgColor
        btn.tag = answerBaseTag+1
        btn.addTarget(self, action: #selector(didClickResponseBtn(btn:)), for: .touchUpInside)
        return btn
    }()
    
    
    private lazy var bottomLine : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: 0xF0F0F0)
        return view
    }()
    
    
    private lazy var additionHoldView : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.contentBkgColor
        view.layer.cornerRadius = kCornerRadius6
        view.clipsToBounds = true
        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(hex:0xFF3A3A), range: starRange)
                titleLabel.attributedText = questionAttrStr
            }
        }
    }
    
    
    
    var model : YHBasicInfoCellModel? {
        didSet {
            updateUI()
        }
    }
}


private extension YHBasicInfoFillView {
    func setupUI() {
        backgroundColor = .clear
        title = "1、是否曾在香港或其他地方因任何罪行或违法行为被定罪?"
        addSubview(titleLabel)
        addSubview(answer2Btn)
        addSubview(answer1Btn)
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(kMargin)
            make.left.right.equalToSuperview()
        }
        
        answer1Btn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: btnWidth, height: btnHeight))
            make.left.equalTo(titleLabel.snp.left)
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
        }
        
        answer2Btn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: btnWidth, height: btnHeight))
            make.left.equalTo(answer1Btn.snp.right).offset(12)
            make.top.equalTo(answer1Btn.snp.top)
        }
        
        addSubview(bottomLine)
        bottomLine.snp.makeConstraints { make in
            make.top.equalTo(answer1Btn.snp.bottom).offset(12)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        addSubview(additionHoldView)
        additionHoldView.isHidden = true
    }
    
    @objc func didClickResponseBtn(btn: UIButton) {
        printLog("点击了按钮哈 \(btn.tag)")
        btn.isSelected = true
        updateAnswerButton(btn, true)
        
        
        if btn == answer1Btn {
            answer2Btn.isSelected = false
            updateAnswerButton(answer2Btn, false)
            
            additionHoldView.snp.removeConstraints()
            bottomLine.snp.remakeConstraints { make in
                make.top.equalTo(answer1Btn.snp.bottom).offset(12)
                make.left.right.equalToSuperview()
                make.height.equalTo(0.5)
                make.bottom.equalToSuperview()
            }
            additionHoldView.isHidden = true
            bottomLine.isHidden = false
            
            model?.answer = "N"
            
        } else {
            answer1Btn.isSelected = false
            updateAnswerButton(answer1Btn, false)
            
            bottomLine.snp.removeConstraints()
            additionHoldView.snp.remakeConstraints { make in
                make.top.equalTo(answer1Btn.snp.bottom).offset(12)
                make.left.right.equalToSuperview()
                make.height.equalTo(78)
                make.bottom.equalToSuperview().offset(-18)
            }
            
            model?.answer = "Y"
            
            
            additionHoldView.isHidden = false
            bottomLine.isHidden = true
        }
        
        
        additionHoldView.layoutIfNeeded()
        bottomLine.layoutIfNeeded()
        
        block?()
    }
    
    
    
    // 更新答案按钮选中状态
    private func updateAnswerButton(_ btn:UIButton, _ isSelect:Bool) {
        btn.layer.borderColor = (isSelect ? btnTitleSelectColor : .clear).cgColor
        btn.setTitleColor((isSelect ? btnTitleSelectColor : btnTitleDefaultColor), for: .normal)
        btn.backgroundColor = (isSelect ? btnBgSelectColor : btnBgDefaultColor)
    }
    
    private func updateUI() {
        guard let model = model else { return }
        title = model.question
        if model.answer == "Y" {
            didClickResponseBtn(btn: answer2Btn)
        } else {
            if model.answer == "N" {
                didClickResponseBtn(btn: answer1Btn)
            }
        }
    }
}
