import UIKit

protocol RoleVoiceListCellDelegate: AnyObject {
    func roleVoiceListTapAdd(_ cell: RoleVoiceListCell, model: VoiceModel?)
    func roleVoiceListTapPlay(_ cell: RoleVoiceListCell, model: VoiceModel?)
}

class RoleVoiceListCell: UITableViewCell {
    var block: UIView!
    var playButton: AudioButton!//EPIconGhostButton!
    var titleLabel: UILabel!
    var subTitleLabel : UILabel!
    var selectMark: EPIconGhostButton!

    var data: VoiceModel?
    weak var delegate: RoleVoiceListCellDelegate?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        backgroundColor = .clear
        selectionStyle = .none
        
        block = {
            let v = UIView()
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.leading.equalToSuperview().offset(CGFloat.lrs)
                make.trailing.equalToSuperview().offset(-CGFloat.lrs)
                make.bottom.equalToSuperview().offset(-16)
            }
            v.backgroundColor = .c.csbn
            v.layer.cornerRadius = 16
            v.clipsToBounds = true
            return v
        }()

        playButton = {
            let v = AudioButton(style: .clearBg)
            //v.addTarget(self, action: #selector(tapVoicePlay(sender:)), for: .touchUpInside)
            v.isUserInteractionEnabled = false
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.leading.equalToSuperview().offset(16)
            }
            return v
        }()

        titleLabel = {
            let v = UILabel()
            v.numberOfLines = 0
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(52)
                make.trailing.equalToSuperview().offset(-64)
                make.top.equalToSuperview().offset(16)
            }
            v.font = .t.tbm
            v.textColor = .c.ctpn
            return v
        }()

        subTitleLabel = {
            let v = UILabel()
            block.addSubview(v)
            v.numberOfLines = 0
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(52)
                make.top.equalTo(titleLabel.snp.bottom).offset(4)
                make.bottom.equalToSuperview().offset(-16)
            }
            v.font = .t.tbsn
            v.textColor = .c.ctsn
            return v
        }()
        

        selectMark = {
            let v = EPIconGhostButton(radius: .none, iconSize: .small, iconCode: .add)
            v.addTarget(self, action: #selector(tapVoiceAdd(sender:)), for: .touchUpInside)
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.trailing.equalToSuperview().offset(-16)
                make.centerY.equalToSuperview()
                make.size.equalTo(v.bgImageSize())
            }
            return v
        }()
//        #warning("test")
//        testData()
    }
    
    private func testData(){
        titleLabel.text = "ABC DEF Voice"
        subTitleLabel.text = "ABC DEF Voice"
    }
    
    public func config(model: VoiceModel){
        data = model
        
        titleLabel.text = model.voiceDict.name
        subTitleLabel.text = model.voiceDict.description//"ABC DEF Voice"
        
        playButton.reloadState(with: model.speechModel)
    }
    
    public func setupSelectedState(selected: Bool){
        if selected{
            block.layer.borderWidth = 2
            block.layer.borderColor = UIColor.c.cpn.cgColor
            selectMark.iconCode = .delete
            selectMark.optionalIconColor = UIColor.c.cpn
        }else{
            block.layer.borderWidth = 0
            selectMark.iconCode = .add
            selectMark.optionalIconColor = UIColor.c.ctpn
        }
    }
    
    // MARK: - Action
    
//    @objc private func tapVoicePlay(sender: UIButton){
//        delegate?.roleVoiceListTapPlay(self, model: self.data)
//    }
    
    @objc private func tapVoiceAdd(sender: UIButton){
        delegate?.roleVoiceListTapAdd(self, model: self.data)
    }
}
