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

import UIKit

class YHFormItemTitleCell: UITableViewCell {

    static let cellReuseIdentifier = "YHFormItemTitleCell"
    private let horizonalGap = 18.0

    let editTag = 9527
    let cancelTag = 9528
    
    var rightClickBlock:((YHFormTitleItemEditType)->Void)?


    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .mainTextColor
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSC_M(ofSize: 17)
        return label
    }()
    
    lazy var subTitleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .labelTextColor2
        label.textAlignment = .left
        label.numberOfLines = 0
        label.font = UIFont.PFSC_R(ofSize: 12)
        return label
    }()
    
    lazy var cancelButton: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("取消操作".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_R(ofSize: 14)
        btn.setTitleColor(.brandMainColor, for: .normal)
        btn.tag = cancelTag
        btn.addTarget(self, action: #selector(didClickOperatorBtn(btn:)), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    lazy var editButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "form_edit"), for: .normal)
        btn.tag = editTag
        btn.addTarget(self, action: #selector(didClickOperatorBtn(btn:)), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    lazy var bottomLine:UIView = {
        let view = UIView()
        view.backgroundColor = .separatorColor
        return view
    }()
    
    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
        contentView.addSubview(titleLabel)
        contentView.addSubview(subTitleLabel)
        contentView.addSubview(bottomLine)
        contentView.addSubview(editButton)
        contentView.addSubview(cancelButton)

        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalTo(subTitleLabel.snp.left).offset(-3)
            make.centerY.equalToSuperview()
        }
        
        subTitleLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
        
        editButton.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(24, 24))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-18)
        }
        
        editButton.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(24, 24))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-18)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(92, 40))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        bottomLine.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(horizonalGap)
            make.right.equalToSuperview().offset(-horizonalGap)
            make.height.equalTo(1.0)
            make.bottom.equalToSuperview()
        }
    }
    
    func showEditType(type: YHFormTitleItemEditType) {
        switch type {
            case .none:
                cancelButton.isHidden = true
                editButton.isHidden = true
            case .canEdit:
                cancelButton.isHidden = true
                editButton.isHidden = false
            case .canCancel:
                cancelButton.isHidden = false
                editButton.isHidden = true
        }
    }
    
    @objc func didClickOperatorBtn(btn:UIButton) {
        
        if let clickBlock = rightClickBlock {
            if btn.tag == editTag {
                clickBlock(.canEdit)
                
            } else if btn.tag == cancelTag {
                clickBlock(.canCancel)
            }
        }
    }
    
}
