//
//  YHMyFileMethodTableViewCell.swift
//  galaxy
//
//  Created by EDY on 2024/3/13.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

enum YHMyFileMethodType: Int {
    case sign = 0 // 签署
    case downLoad = 1 // 下载
    case look = 2 // 查看
}

class YHMyFileMethodTableViewCell: UITableViewCell {
    typealias Block = (YHMyFileMethodType) -> ()
    var block: Block?
    var centerView: UIView!
    var titleLabel: UILabel!
    var subTitleLabel: UILabel!
    var signButton: UIButton!
    var lookButton: UIButton!
    var downLoadButton: UIButton!
    var dataSource: YHMySignatureModel? {
        didSet {
            updateAllViews()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        setupUI()
    }
    
    func setupUI() {
        backgroundColor = .clear
        centerView = {
            let view = UIView()
            view.backgroundColor = .white
            view.layer.cornerRadius = kCornerRadius6
            return view
        }()
        contentView.addSubview(centerView)
        centerView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(7)
            make.bottom.equalTo(-7)
        }
        
        titleLabel = {
            let label = UILabel()
            label.font = UIFont.PFSC_M(ofSize: 18)
            label.textColor = UIColor.mainTextColor
            label.text = "电子签"
            return label
        }()
        centerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(16)
            make.height.equalTo(21)
        }
        
        subTitleLabel = {
            let label = UILabel()
            label.font = UIFont.PFSC_R(ofSize: 16)
            label.textColor = UIColor.labelTextColor2
            label.textAlignment = .left
            label.text = "申明书.doc"
            return label
        }()
        centerView.addSubview(subTitleLabel)
        subTitleLabel.snp.makeConstraints { make in
            make.height.equalTo(20)
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(80)
        }
        
        downLoadButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor = UIColor.brandMainColor.withAlphaComponent(0.08)
            button.setTitleColor(UIColor.brandMainColor, for: .normal)
            button.layer.cornerRadius = 4
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.brandMainColor.cgColor
            button.isSelected = true
            button.titleLabel?.font = UIFont.PFSC_M(ofSize: 14)
            button.setTitle("下载", for: .normal)
            button.addTarget(self, action: #selector(downLoadButtonClick), for: .touchUpInside)
            return button
        }()
        addSubview(downLoadButton)
        downLoadButton.snp.makeConstraints { make in
            make.right.bottom.equalTo(-25)
            make.height.equalTo(20)
            make.width.equalTo(55)
        }
        
        lookButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor = UIColor.brandMainColor.withAlphaComponent(0.08)
            button.setTitleColor(UIColor.brandMainColor, for: .selected)
            button.setTitleColor(UIColor.mainTextColor, for: .normal)
            button.layer.cornerRadius = 4
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.brandMainColor.cgColor
            button.titleLabel?.font = UIFont.PFSC_M(ofSize: 14)
            button.isSelected = true
            button.setTitle("查看", for: .normal)
            button.addTarget(self, action: #selector(lookButtonClick), for: .touchUpInside)
            return button
        }()
        addSubview(lookButton)
        lookButton.snp.makeConstraints { make in
            make.right.equalTo(downLoadButton.snp.left).offset(-6)
            make.centerY.equalTo(downLoadButton.snp.centerY)
            make.height.equalTo(20)
            make.width.equalTo(55)
        }
        
        signButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor = UIColor.brandMainColor.withAlphaComponent(0.08)
            button.setTitleColor(UIColor.brandMainColor, for: .selected)
            button.setTitleColor(UIColor.mainTextColor, for: .normal)
            button.layer.cornerRadius = 4
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.brandMainColor.cgColor
            button.isSelected = true
            button.titleLabel?.font = UIFont.PFSC_M(ofSize: 14)
            button.setTitle("签署", for: .normal)
            button.addTarget(self, action: #selector(signButtonClick), for: .touchUpInside)
            return button
        }()
        addSubview(signButton)
        signButton.snp.makeConstraints { make in
            make.right.equalTo(lookButton.snp.left).offset(-6)
            make.centerY.equalTo(lookButton.snp.centerY)
            make.height.equalTo(20)
            make.width.equalTo(55)
        }
    }
    
    func updateAllViews() {
        guard let dataSource = dataSource else { return }
        titleLabel.text = dataSource.doc_name
        subTitleLabel.text = "\(dataSource.doc_type)"
    }

    @objc func downLoadButtonClick() {
        if let block = block {
            block(.downLoad)
        }
    }
    
    @objc func lookButtonClick() {
        if let block = block {
            block(.look)
        }
    }
    
    @objc func signButtonClick() {
        if let block = block {
            block(.sign)
        }
    }
}

