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

import UIKit

// 选择项
class YHFormChoiceItem {
    
    var title:String
    // 是否被选中
    var isSelect:Bool
    
    init(title: String, isSelect: Bool) {
        self.title = title
        self.isSelect = isSelect
    }
}

class YHFormItemDoubleChoiceCell: UITableViewCell {
    
    static let cellReuseIdentifier = "YHFormItemDoubleChoiceCell"
    let btnWidth = 70.0
    let btnHeight = 32.0
    let btnTitleSelectColor = UIColor(hex: 0x4487F9)
    let btnTitleDefaultColor = UIColor(hex:0x222222)
    let btnBgDefaultColor = UIColor(hex:0xF8F9FB)
    let btnBgSelectColor = UIColor(hex:0x4487F9, alpha: 0.08)
    let answerBaseTag  = 9527
    
    var answerBlock:(([YHFormChoiceItem], Int)->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
            }
        }
    }
    
    // 必须传2个item的arr
    var answerArr:[YHFormChoiceItem]? {
        didSet {
            if let answerArr = answerArr, answerArr.count == 2 {
                
                for i in 0...1 {
                    let answerBtn = self.viewWithTag(answerBaseTag+i) as! UIButton
                    let item = answerArr[i]
                    answerBtn.setTitle(item.title, for: .normal)
                    updateAnswerButton(answerBtn, item.isSelect)
                }
            } else {
                // 重置所有答案状态
                for i in 0...1 {
                    let answerBtn = self.viewWithTag(answerBaseTag+i) as! UIButton
                    answerBtn.setTitle("", for: .normal)
                    updateAnswerButton(answerBtn, false)
                }
            }
        }
    }
    
    // 更新答案按钮选中状态
    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)
    }
    
    lazy var titleLabel: 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 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
    }()
    
    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
    }()

    
    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(answer2Btn)
        contentView.addSubview(answer1Btn)
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(contentView.snp.top).offset(22)
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(answer1Btn.snp.left).offset(-8)
            make.bottom.equalTo(contentView.snp.bottom).offset(-22)
        }
        
        answer2Btn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: btnWidth, height: btnHeight))
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(16)
        }
        
        answer1Btn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: btnWidth, height: btnHeight))
            make.right.equalTo(answer2Btn.snp.left).offset(-8)
            make.top.equalTo(answer2Btn)
        }
    }
    
    @objc func didClickResponseBtn(btn: UIButton) {
        
        let selectIndex = btn.tag-answerBaseTag
        if let resultArr = answerArr, resultArr.count == 2 {
            
            for i in 0...1 {
                let item = resultArr[i]
                item.isSelect = (i == selectIndex)
            }
            answerArr = resultArr
            if let answerBlock = answerBlock {
                answerBlock(resultArr, selectIndex)
            }
        }
    }
}
