//
//  YHWorkExperiencePositionTableViewCell.swift
//  galaxy
//
//  Created by EDY on 2024/11/15.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import AttributedString

class YHWorkExperiencePositionTableViewCell: UITableViewCell {
    typealias ExperienceListBlock = (_ model: YHWorkExperiencePositionModel) -> Void
    typealias IsMoreBlock = (_ isMore: Bool) -> Void
    typealias NewWorkBlock = () -> Void
    var clickBlock: ExperienceListBlock?
    var deleteBlock: ExperienceListBlock?
    var isMoreBlock: IsMoreBlock?
    var newWorkBlock: NewWorkBlock?
    var centerView: UIView!
    var titleLabel: UILabel!
    var subTitleLabel: UILabel!
    var cannalButton: UIButton!
    var moreButton: UIButton!
    var mainItemView: UIView!
    var bottomView: YHWorkItemAddView!
    var showPrompt: Bool = false
    var dataSource: [YHWorkExperiencePositionModel]? {
        didSet {
            updateAllViews()
        }
    }

    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(14)
            make.bottom.equalTo(-7)
        }
        
        titleLabel = {
            let label = UILabel()
            let a: ASAttributedString = .init("*", .font(UIFont.PFSC_R(ofSize: 17)), .foreground(UIColor.failColor))
            let b: ASAttributedString = .init("职位信息", .font(UIFont.PFSC_R(ofSize: 17)), .foreground(UIColor.mainTextColor))
            label.attributed.text = a + b
            return label
        }()
        centerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(23)
            make.height.equalTo(24)
            make.right.equalTo(-20)
        }
        
        subTitleLabel = {
            let label = UILabel()
            label.font = UIFont.PFSC_R(ofSize: 12)
            label.text = "(从该公司第一个职位开始， 直至最后一个职位）"
            label.textColor = UIColor.labelTextColor2
            return label
        }()
        centerView.addSubview(subTitleLabel)
        subTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(50)
            make.height.equalTo(20)
            make.right.equalTo(-20)
        }
        
        cannalButton = {
            let button = UIButton(type: .custom)
            button.titleLabel?.font = UIFont.PFSC_M(ofSize: 14)
            button.contentHorizontalAlignment = .center
            button.setTitle("取消编辑", for: .normal)
            button.setTitleColor(UIColor.brandMainColor, for: .normal)
            button.addTarget(self, action: #selector(cannal), for: .touchUpInside)
            button.isHidden = true
            return button
        }()
        centerView.addSubview(cannalButton)
        cannalButton.snp.makeConstraints { make in
            make.right.equalTo(-18)
            make.top.equalTo(24)
            make.height.equalTo(20)
            make.width.equalTo(60)
        }
        
        moreButton = {
            let button = UIButton(type: .custom)
            button.titleLabel?.font = UIFont.PFSC_M(ofSize: 14)
            button.contentHorizontalAlignment = .right
            button.setTitle("编辑", for: .normal)
            button.setTitleColor(UIColor.brandMainColor, for: .normal)
            button.addTarget(self, action: #selector(more), for: .touchUpInside)
            return button
        }()
        centerView.addSubview(moreButton)
        moreButton.snp.makeConstraints { make in
            make.right.equalTo(-18)
            make.top.equalTo(24)
            make.height.equalTo(20)
            make.width.equalTo(60)
        }
        
        mainItemView = {
            let view = UIView()
            view.backgroundColor = .white
            view.layer.cornerRadius = kCornerRadius6
            return view
        }()
        centerView.addSubview(mainItemView)
        mainItemView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-65)
            make.top.equalTo(85)
        }
        
        bottomView = {
            let view = YHWorkItemAddView()
            view.addBtn.setTitle("新增职位".local, for: .normal)
            view.clickBlock = {[weak self] in
                guard let self = self else { return }
                if let block = self.newWorkBlock {
                    block()
                }
            }
            return view
        }()
        centerView.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.right.bottom.equalTo(-18)
            make.left.equalTo(18)
            make.height.equalTo(45)
        }
    }
    
    func updateAllViews() {
        mainItemView.removeSubviews()
        guard let dataSource = dataSource else {
            let line = UIView()
            line.backgroundColor = UIColor.separatorColor
            mainItemView.addSubview(line)
            line.snp.makeConstraints { make in
                make.left.equalTo(18)
                make.top.equalToSuperview()
                make.height.equalTo(1)
                make.right.equalTo(-18)
            }
            let itemView = YHWorkItemView()
            itemView.dataSource = YHWorkItemListModel()
            mainItemView.addSubview(itemView)
            itemView.snp.makeConstraints { make in
                make.left.equalTo(18)
                make.top.equalTo(1)
                make.height.equalTo(51)
                make.right.equalTo(-18)
            }
            return
        }
        var total = 0
        for i in 0 ..< dataSource.count {
            let line = UIView()
            line.backgroundColor = UIColor.separatorColor
            mainItemView.addSubview(line)
            line.snp.makeConstraints { make in
                make.left.equalTo(18)
                make.top.equalTo(total)
                make.height.equalTo(1)
                make.right.equalTo(-18)
            }
            total += 1
            let itemView = YHWorkExperiencePositionItemView()
            itemView.clickBlock = {[weak self] model in
                guard let self = self else { return }
                if let block = self.clickBlock {
                    block(model)
                }
            }
            
            itemView.deleteBlock = {[weak self] model in
                guard let self = self else { return }
                if let block = self.deleteBlock {
                    block(model)
                }
            }
            let model = dataSource[i]
            itemView.tag = 4000 + i
            itemView.dataSource = dataSource[i]
            itemView.showPrompt = showPrompt
            var h = 0
            if model.position_name == "" {
                h = 51
            } else {
                let text = model.position_name // 要显示的文本内容
                let font = UIFont.PFSC_B(ofSize: 14) // 字体大小
                let maxWidth = 143.0 // 最大宽度限制
                
                // 创建NSAttributedString对象并设置属性
                let attributes = [NSAttributedString.Key.font: font] as [NSAttributedString.Key: Any]
                _ = NSMutableAttributedString(string: text, attributes: attributes)
                 
                // 根据指定的最大宽度和字体大小计算文本的高度
                let size = (text as NSString).boundingRect(with: CGSize(width: maxWidth, height: .greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: attributes, context: nil).size
                if size.height > 20 {
                    h = 71
                } else {
                    h = 51
                }
            }
            if showPrompt && model.missing_item_num != 0 {
                h += 20
            }
            mainItemView.addSubview(itemView)
            itemView.snp.makeConstraints { make in
                make.left.equalTo(18)
                make.top.equalTo(total)
                make.height.equalTo(h)
                make.right.equalTo(-18)
            }
            total += h
        }
    }
    
    @objc func cannal() {
        moreButton.isHidden = false
        cannalButton.isHidden = true
        for i in 0..<(dataSource?.count ?? 0) {
            guard let view = mainItemView.viewWithTag(4000 + i) as? YHWorkExperiencePositionItemView else { return }
            view.isShowDelete(i, isShow: false)
        }
        if let block = isMoreBlock {
            block(false)
        }
    }
    
    @objc func more() {
        moreButton.isHidden = true
        cannalButton.isHidden = false
        for i in 0..<(dataSource?.count ?? 0) {
            guard let view = mainItemView.viewWithTag(4000 + i) as? YHWorkExperiencePositionItemView else { return }
            view.isShowDelete(i, isShow: true)
        }
        if let block = isMoreBlock {
            block(true)
        }
    }
}
