//
//  LNAutoReplyItemCell.swift
//  Gami
//
//  Created by OneeChan on 2026/3/9.
//

import Foundation
import UIKit
import SnapKit


class LNAutoReplyItemCell: UITableViewCell {
    private let titleLabel = UILabel()
    private let stateLabel = UILabel()
    private let contentLabel = UILabel()
    private let voiceView = LNVoicePlayView()
    
    private var curNum: Int = 0
    private var curItem: LNAutoReplyVO?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupViews()
    }
    
    func update(_ num: Int, item: LNAutoReplyVO) {
        titleLabel.text = .init(key: "B00114", num + 1)
        switch item.status {
        case .reviewing:
            stateLabel.textColor = .init(hex: "#FFB836")
            stateLabel.text = .init(key: "B00003")
        case .pass:
            stateLabel.textColor = .text_5
            stateLabel.text = .init(key: "B00118")
        case .reject:
            stateLabel.textColor = .fill_6
            stateLabel.text = .init(key: "A00140")
        }
        
        contentLabel.isHidden = true
        voiceView.isHidden = true
        switch item.type {
        case .text:
            contentLabel.isHidden = false
            contentLabel.text = item.textContent
        case .voice:
            voiceView.isHidden = false
            voiceView.update(item.voiceUrl, duration: item.voiceDuration)
        }
        
        curNum = num
        curItem = item
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNAutoReplyItemCell {
    private func setupViews() {
        backgroundColor = .clear
        
        let container = UIView()
        container.layer.cornerRadius = 12
        container.backgroundColor = .fill
        container.onTap { [weak self] in
            guard let self else { return }
            guard let curItem else { return }
            if curItem.status == .reviewing {
                pushToSkillReview()
            } else {
                pushToAutoRelyEdit(num: curNum, item: curItem)
            }
        }
        contentView.addSubview(container)
        container.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview()
        }
        
        let header = buildHeader()
        container.addSubview(header)
        header.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        let content = buildContent()
        container.addSubview(content)
        content.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalToSuperview().offset(-9)
            make.top.equalTo(header.snp.bottom).offset(6)
            make.bottom.equalToSuperview().offset(-8)
        }
    }
    
    private func buildHeader() -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(30)
        }
        
        titleLabel.font = .heading_h5
        titleLabel.textColor = .text_5
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(12)
        }
        
        stateLabel.font = .heading_h5
        stateLabel.textColor = .text_5
        container.addSubview(stateLabel)
        stateLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-12)
        }
        
        return container
    }
    
    private func buildContent() -> UIView {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.spacing = 30
        stackView.alignment = .center
        
        contentLabel.isHidden = true
        contentLabel.font = .body_s
        contentLabel.textColor = .text_5
        contentLabel.numberOfLines = 2
        stackView.addArrangedSubview(contentLabel)
        
        voiceView.isHidden = true
        voiceView.layer.cornerRadius = 16
        voiceView.playIcon.snp.updateConstraints { make in
            make.width.height.equalTo(22)
        }
        voiceView.waveView.snp.updateConstraints { make in
            make.width.equalTo(19)
            make.height.equalTo(11)
        }
        voiceView.waveView.build()
        voiceView.snp.makeConstraints { make in
            make.width.equalTo(123)
            make.height.equalTo(32)
        }
        stackView.addArrangedSubview(voiceView)
        
        let emptyView = UIView()
        emptyView.snp.makeConstraints { make in
            make.width.equalTo(0).priority(.low)
            make.height.equalTo(0)
        }
        stackView.addArrangedSubview(emptyView)
        
        let arrow = UIImageView.arrowImageView(size: 10)
        arrow.tintColor = .text_2
        arrow.setContentHuggingPriority(.required, for: .horizontal)
        arrow.setContentCompressionResistancePriority(.required, for: .horizontal)
        stackView.addArrangedSubview(arrow)
        
        return stackView
    }
}
