//
//  YHResourceTableViewCell.swift
//  galaxy
//
//  Created by alexzzw on 2025/9/24.
//  Copyright © 2025 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import Kingfisher
import AttributedString

class YHResourceTableViewCell: UITableViewCell {
    static let labelFont = UIFont.PFSC_R(ofSize: 14)
    static let labelHeight: CGFloat = 20.0
    static let marginX: CGFloat = 20.0
    static let logoWidth: CGFloat = 60.0
    static let logoToRight: CGFloat = 10.0
    static let marginBetweenVLine: CGFloat = 6.0
    static let widthVLine: CGFloat = 1
    static let arrowWidth: CGFloat = 24
    static let arrowToLeft: CGFloat = 24
    
    static let cellReuseIdentifier = "YHResourceTableViewCell"
    
    // MARK: - 属性
    var resourceModel: YHResourceListModel? {
        didSet {
            updateUI()
        }
    }
        
    // MARK: - UI Components
    
    // 分割线
    lazy var separatorLine: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.brandGrayColor3
        return view
    }()
    
    // 左侧LOGO
    lazy var logoImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 4
        imageView.clipsToBounds = true
        imageView.layer.borderColor = UIColor.brandGrayColor3.cgColor
        imageView.layer.borderWidth = 0.5
        return imageView
    }()
    
    lazy var typeTagIcon: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    lazy var rightArrow: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "resource_feed_arrow"))
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    // 主标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.PFSC_M(ofSize: 15)
        label.textColor = UIColor.brandGrayColor8
        label.numberOfLines = 2
        return label
    }()
    
    // 公司名称&行业复合显示
    lazy var companyDetailLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.PFSC_R(ofSize: 12)
        label.textColor = UIColor.brandGrayColor7
        label.numberOfLines = 2
        label.textAlignment = .left
        return label
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - 私有方法
extension YHResourceTableViewCell {
    
    private func setupUI() {
        backgroundColor = .white
        selectionStyle = .none
        
        contentView.addSubview(logoImageView)
        contentView.addSubview(rightArrow)
        contentView.addSubview(typeTagIcon)
        contentView.addSubview(titleLabel)
        contentView.addSubview(separatorLine)
        contentView.addSubview(companyDetailLabel)
        
        setupConstraints()
    }
    
    private func setupConstraints() {
        // LOGO约束
        logoImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(YHResourceTableViewCell.marginX)
            make.top.equalToSuperview().offset(24)
            make.width.height.equalTo(YHResourceTableViewCell.logoWidth)
            make.bottom.lessThanOrEqualToSuperview().offset(-32)
        }
                
        // 右上角标签约束
        typeTagIcon.snp.makeConstraints { make in
            make.left.equalTo(logoImageView.snp.right).offset(YHResourceTableViewCell.logoToRight)
            make.top.equalTo(logoImageView)
            make.height.equalTo(16)
            make.width.equalTo(52)
        }
        
        rightArrow.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-YHResourceTableViewCell.marginX)
            make.width.height.equalTo(YHResourceTableViewCell.arrowWidth)
            make.top.equalTo(typeTagIcon.snp.bottom).offset(4)
        }
        
        // 主标题约束
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(logoImageView.snp.right).offset(YHResourceTableViewCell.logoToRight)
            make.right.equalTo(rightArrow.snp.left)
            make.top.equalTo(typeTagIcon.snp.bottom).offset(4)
        }
        
        // 公司名称约束
        companyDetailLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.bottom.lessThanOrEqualToSuperview().offset(-32)
            make.right.equalTo(rightArrow.snp.left)
        }
        
        let pixelOne = 1.0 / UIScreen.main.scale
        // 分割线约束
        separatorLine.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(pixelOne)
        }
    }
    
    static func getLabelWidth(_ text: String) -> CGFloat {
        let attrString = NSAttributedString(string: text, attributes: [.font: YHResourceTableViewCell.labelFont])
        let width = attrString.yh_width(containerHeight: YHResourceTableViewCell.labelHeight)
        return width
    }
    
    private func updateUI() {
        guard let model = resourceModel else { return }
        
        // 设置基本信息 - 使用新的API字段
        titleLabel.text = getServiceOrDemandText(model: model)  // 显示服务或需求信息
        let companyName = model.displayName.count > 20 ? model.displayName.prefix(20) + "..." : model.displayName
        let industry = model.industryDisplayText.count > 20 ? model.industryDisplayText.prefix(20) + "..." : model.industryDisplayText
        
        let tipsIcon = ASAttributedString.init(.image(UIImage(named: "company_detail_vline") ?? UIImage(), .custom(.offset(CGPoint(x: 0, y: -2.0)), size: .init(width: 1, height: 8))))
        let contentAtt = (ASAttributedString(string: "\(companyName)  ", .foreground(UIColor.brandGrayColor7), .font(UIFont.PFSC_R(ofSize: 12))) + tipsIcon + ASAttributedString(string: "  \(industry)", .foreground(UIColor.brandGrayColor7), .font(UIFont.PFSC_R(ofSize: 12))))
        companyDetailLabel.attributed.text = contentAtt
        // 设置Logo - 使用新的API字段
        if !model.companyLogoUrl.isEmpty {
            logoImageView.kf.setImage(
                with: URL(string: model.companyLogoUrl),
                placeholder: UIImage(named: "global_default_image")
            )
        } else {
            logoImageView.image = UIImage(named: "global_default_image")
        }
        
        // 设置类型标签 - 根据是服务还是需求
        if model.isService {
            typeTagIcon.image = UIImage(named: "resource_flag_service")
        } else {
            typeTagIcon.image = UIImage(named: "resource_flag_demand")
        }
    }
    
    /// 获取服务或需求文本
    private func getServiceOrDemandText(model: YHResourceListModel) -> String {
        if model.isService {
            return model.resource_provided.isEmpty ? "-" : model.resource_provided
        } else {
            return model.demand_published.isEmpty ? "-" : model.demand_published
        }
    }
}
