//
//  LNIMOfficialMessageCell.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/11.
//

import Foundation
import UIKit
import SnapKit
import AutoCodable

class LNIMOfficialMessageCell: UITableViewCell {
    private let topCover = UIImageView()
    private let titleLabel = UILabel()
    private let descLabel = UILabel()
    private let jumpView = UIView()
    
    private var curItem: LNIMOfficialMessage?
    
    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")
    }
    
    func update(_ data: LNIMOfficialMessage) {
        topCover.isHidden = data.image.isEmpty
        topCover.sd_setImage(with: URL(string: data.image))
        titleLabel.text = data.title
        descLabel.text = data.content
        
        jumpView.isHidden = data.link.isEmpty
        
        curItem = data
    }
}

extension LNIMOfficialMessageCell {
    private func setupViews() {
        backgroundColor = .clear
        
        let container = UIView()
        container.layer.cornerRadius = 12
        container.clipsToBounds = true
        container.backgroundColor = .fill
        contentView.addSubview(container)
        container.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-10)
        }
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-12)
        }
        
        topCover.contentMode = .scaleAspectFill
        topCover.clipsToBounds = true
        stackView.addArrangedSubview(topCover)
        topCover.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.height.equalTo(topCover.snp.width).multipliedBy(130.0/343)
        }
        
        stackView.addArrangedSubview(buildTitleView())
        stackView.addArrangedSubview(buildJumpView())
    }
    
    private func buildTitleView() -> UIView {
        let container = UIView()
        
        titleLabel.font = .heading_h4
        titleLabel.textColor = .text_5
        titleLabel.numberOfLines = 0
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalToSuperview().offset(12)
        }
        
        descLabel.font = .body_s
        descLabel.textColor = .text_3
        descLabel.numberOfLines = 0
        container.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
            make.bottom.equalToSuperview()
        }
        
        return container
    }
    
    private func buildJumpView() -> UIView {
        jumpView.onTap { [weak self] in
            guard let self else { return }
            guard let url = curItem?.link, !url.isEmpty else { return }
            pushToWebView(.init(url: url))
        }
        
        let line = UIView()
        line.backgroundColor = .fill_2
        jumpView.addSubview(line)
        line.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalToSuperview().offset(6)
            make.height.equalTo(0.5)
        }
        
        let jumpTitle = UILabel()
        jumpTitle.text = .init(key: "A00098")
        jumpTitle.font = .heading_h4
        jumpTitle.textColor = .text_6
        jumpView.addSubview(jumpTitle)
        jumpTitle.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(6)
            make.leading.equalToSuperview().offset(12)
            make.bottom.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 14)
        arrow.tintColor = .text_4
        jumpView.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.centerY.equalTo(jumpTitle)
            make.trailing.equalToSuperview().offset(-12)
        }
        
        return jumpView
    }
}
