//
//  LNProfileStaringPanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/2.
//

import Foundation
import UIKit
import SnapKit


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

extension LNProfileStaringPanel: LNFiveStarScoreViewDelegate {
    func onFiveStarScoreView(view: LNFiveStarScoreView, scoreChanged newScore: Double) {
        let bg = newScore == 0 ? nil : UIImage.primary_8
        confirmButton.setBackgroundImage(bg, for: .normal)
    }
}

extension LNProfileStaringPanel {
    private func setupViews() {
        avatar.backgroundColor = .fill
        avatar.layer.cornerRadius = 30
        avatar.layer.borderColor = UIColor.fill.cgColor
        avatar.layer.borderWidth = 2
        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: "A00194")
        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.spacing = 12
        scoreView.delegate = self
        container.addSubview(scoreView)
        scoreView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(17)
        }
        
        confirmButton.setTitle(.init(key: "A00183"), for: .normal)
        confirmButton.backgroundColor = .text_2
        confirmButton.setTitleColor(.text_1, for: .normal)
        confirmButton.titleLabel?.font = .heading_h3
        confirmButton.layer.cornerRadius = 23.5
        confirmButton.clipsToBounds = true
        confirmButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            dismiss()
            handler?(scoreView.score)
        }), for: .touchUpInside)
        container.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(12)
            make.top.equalTo(scoreView.snp.bottom).offset(25)
            make.bottom.equalToSuperview().offset(commonBottomInset)
            make.height.equalTo(47)
        }
    }
}

#if DEBUG

import SwiftUI

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

#Preview(body: {
    LNProfileStaringPanelPreview()
})
#endif
