//
//  YHCheckQuestionCell.swift
//  galaxy
//
//  Created by edy on 2024/1/22.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit




class YHFormItemQuestionCell: UITableViewCell {
    
     static let cellReuseIdentifier = "YHFormItemQuestionCell"
     let btnWidth = 70.0
     let btnHeight = 32.0
     let btnTitleSelectColor = UIColor(hex: 0x4487F9)
     let btnTitleDefaultColor = UIColor(hex:0x222222)
     let btnBgColor = UIColor(hex:0xF8F9FB)
     let confirmTag = 9527
     let negativeTag = 9528
    
     var responseBlock:((Bool)->Void)?
    
     var title:String? {
         didSet {
            
            if let question = title {
                let str = "*"+question
                let attributes: [NSAttributedString.Key: Any] = [
                    .font: UIFont.PFSC_R(ofSize: 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
            }
        }
    }
    
    var response:Bool = false {
        didSet {
            showResponse(response)
        }
    }

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor(hexString:"#333333")
        label.textAlignment = .left
        label.font = UIFont.PFSC_R(ofSize: 14)
        return label
    }()
    
    lazy var confirmBtn: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("是".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_R(ofSize: 14)
        btn.setTitleColor(btnTitleDefaultColor, for: .normal)
        btn.backgroundColor = btnBgColor
        btn.layer.cornerRadius = btnHeight/2.0
        btn.layer.masksToBounds = true
        btn.layer.borderWidth = 1
        btn.tag = confirmTag
        btn.addTarget(self, action: #selector(didClickResponseBtn(btn:)), for: .touchUpInside)
        return btn
    }()
    
    lazy var negativeBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("否".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_R(ofSize: 14)
        btn.setTitleColor(btnTitleDefaultColor, for: .normal)
        btn.backgroundColor = btnBgColor
        btn.layer.cornerRadius = btnHeight/2.0
        btn.layer.masksToBounds = true
        btn.layer.borderWidth = 1
        btn.tag = negativeTag
        btn.addTarget(self, action: #selector(didClickResponseBtn(btn:)), for: .touchUpInside)
        return btn
    }()

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    func setupUI() {
        
        self.selectionStyle = .none
        
        title = "  1、是否曾在香港或其他地方因任何罪行或违法行为被定罪？是否曾在香港或其他地方因任何罪行或违法行为被定罪？是否曾在香港或其他地方因任何罪行或违法行为被定罪？是否曾在香港或其他地方因任何罪行或违法行为被定罪？是否曾在香港或其他地方因任何罪行或违法行为被定罪？ "
        contentView.addSubview(titleLabel)
        contentView.addSubview(negativeBtn)
        contentView.addSubview(confirmBtn)
        // 默认选择否按钮
        response = false
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(contentView.snp.top).offset(12)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
        }
        
        negativeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: btnWidth, height: btnHeight))
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        confirmBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: btnWidth, height: btnHeight))
            make.left.equalTo(negativeBtn.snp.right).offset(12)
            make.centerY.equalTo(negativeBtn)
        }
    }
    
    @objc func didClickResponseBtn(btn: UIButton) {
        response = (btn.tag == confirmTag)
        if let responseBlock = responseBlock {
            responseBlock(btn.tag==confirmTag)
        }
    }
    
    func showResponse(_ isConfirm: Bool) {
        confirmBtn.layer.borderColor = (isConfirm ? btnTitleSelectColor : .clear).cgColor
        confirmBtn.setTitleColor((isConfirm ? btnTitleSelectColor : btnTitleDefaultColor), for: .normal)
        negativeBtn.layer.borderColor = (isConfirm ? .clear : btnTitleSelectColor).cgColor
        negativeBtn.setTitleColor((isConfirm ?  btnTitleDefaultColor: btnTitleSelectColor), for: .normal)
    }
}
