//
//  LNOrderCommentPanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/19.
//

import Foundation
import UIKit
import SnapKit


class LNOrderCommentPanel: LNPopupView {
    var handler: ((Double, String) -> Void)? = nil
    
    private let avatar = UIImageView()
    private let titleLabel = UILabel()
    private let descLabel = UILabel()
    private let starLabel = UILabel()
    private let scoreView = LNFiveStarScoreView()
    private let commentView = LNCommonTextView()
    private let confirmButton = UIButton()
    
    private var curOrderId: String?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    func update(_ url: String, orderId: String) {
        avatar.sd_setImage(with: URL(string: url))
        curOrderId = orderId
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNOrderCommentPanel: LNFiveStarScoreViewDelegate {
    func onFiveStarScoreView(view: LNFiveStarScoreView, scoreChanged newScore: Double) {
        let bg = newScore == 0 ? nil : UIImage.primary_8
        starLabel.text = "\(newScore)"
        confirmButton.setBackgroundImage(bg, for: .normal)
        starLabel.textColor = .text_5
        descLabel.textColor = .text_5
        starLabel.font = .heading_h3
    }
}

extension LNOrderCommentPanel {
    private func setupViews() {
        avatar.layer.cornerRadius = 30
        avatar.layer.borderColor = UIColor.fill.cgColor
        avatar.layer.borderWidth = 2
        avatar.backgroundColor = .fill
        avatar.clipsToBounds = true
        container.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(-16)
            make.width.height.equalTo(60)
        }
        
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = .init(key: "A00166")
        titleLabel.textAlignment = .center
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(avatar.snp.bottom).offset(11)
        }
        
        scoreView.startType = .whiteBorder
        scoreView.icSize = 30
        scoreView.editable = true
        scoreView.unit = 0.5
        scoreView.spacing = 12
        scoreView.delegate = self
        container.addSubview(scoreView)
        scoreView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(17)
        }
        
        descLabel.font = .heading_h5
        descLabel.textColor = .text_2
        descLabel.text = .init(key: "A00192")
        container.addSubview(descLabel)
        descLabel.snp.makeConstraints { make in
            make.centerY.equalTo(scoreView)
            make.leading.equalToSuperview().offset(20)
            make.trailing.lessThanOrEqualTo(scoreView.snp.leading).offset(-12)
        }
        
        starLabel.font = .heading_h5
        starLabel.textColor = .text_2
        starLabel.text = .init(key: "A00193")
        container.addSubview(starLabel)
        starLabel.snp.makeConstraints { make in
            make.centerY.equalTo(scoreView)
            make.trailing.equalToSuperview().offset(-20)
            make.leading.greaterThanOrEqualTo(scoreView.snp.leading).offset(12)
        }
        
        commentView.maxInput = LNOrderManager.orderCommentMaxLength
        commentView.placeholderLabel.text = .init(key: "A00182")
        container.addSubview(commentView)
        commentView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalTo(scoreView.snp.bottom).offset(12)
            make.height.equalTo(90)
        }
        
        confirmButton.setTitle(.init(key: "A00183"), for: .normal)
        confirmButton.titleLabel?.font = .heading_h3
        confirmButton.backgroundColor = .text_2
        confirmButton.setTitleColor(.text_1, for: .normal)
        confirmButton.layer.cornerRadius = 23.5
        confirmButton.clipsToBounds = true
        confirmButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curOrderId else { return }
            LNOrderManager.shared.commentOrder(
                orderId: curOrderId,
                star: scoreView.score,
                comment: commentView.text)
            { [weak self] success in
                guard let self else { return }
                guard success else { return }
                self.dismiss()
                handler?(scoreView.score, commentView.text)
            }
        }), for: .touchUpInside)
        container.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(commentView.snp.bottom).offset(25)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(47)
        }
    }
}

#if DEBUG

import SwiftUI

struct LNOrderCommentPanelPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNOrderCommentPanel()
        view.popup(container)
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

#Preview(body: {
    LNOrderCommentPanelPreview()
})

#endif
