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

import UIKit

class YHDoubleChoiceFormCell: UITableViewCell {
    
    static let cellReuseIdentifier = "YHDoubleChoiceFormCell"
    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 question:String? {
        didSet {
            
            if let question = question {
                let str = "*"+question
                let attributes: [NSAttributedString.Key: Any] = [
                    .font: UIFont.PFSCR(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)
                questionLabel.attributedText = questionAttrStr
            }
            
        }
    }

    lazy var questionLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString:"#333333")
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    lazy var confirmBtn: UIButton = {
        let btn = UIButton.bs_button(title: "是".local, font: UIFont.PFSCR(ofSize: 14), normalColor: btnTitleDefaultColor)
        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.bs_button(title: "否".local, font: UIFont.PFSCR(ofSize: 14), normalColor: btnTitleDefaultColor)
        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

        question = "  1、是否曾在香港或其他地方因任何罪行或违法行为被定罪？是否曾在香港或其他地方因任何罪行或违法行为被定罪？是否曾在香港或其他地方因任何罪行或违法行为被定罪？是否曾在香港或其他地方因任何罪行或违法行为被定罪？是否曾在香港或其他地方因任何罪行或违法行为被定罪？ "
        
        contentView.addSubview(questionLabel)
        contentView.addSubview(negativeBtn)
        contentView.addSubview(confirmBtn)
        // 默认选择否按钮
        didClickResponseBtn(btn: negativeBtn)
        
        questionLabel.snp.makeConstraints { make in
            make.top.equalTo(contentView.snp.top).offset(22)
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(confirmBtn.snp.left).offset(-8)
            make.bottom.equalTo(contentView.snp.bottom).offset(-22)
        }
        
        negativeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: btnWidth, height: btnHeight))
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(16)
        }
        
        confirmBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: btnWidth, height: btnHeight))
            make.right.equalTo(negativeBtn.snp.left).offset(-8)
            make.top.equalTo(negativeBtn)
        }
    }
    
    @objc func didClickResponseBtn(btn: UIButton) {
        let isConfirm = (btn.tag == confirmTag)
        
        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)
        
    }

}
