//
//  LNProfileScoreFloatingView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/16.
//

import Foundation
import UIKit
import SnapKit


class LNProfileScoreFloatingView: UIView {
    private var curDetail: LNUserProfileVO?
    private let background = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        setupGesture()
    }
    
    func update(_ detail: LNUserProfileVO) {
        curDetail = detail
        isHidden = !detail.playmate || detail.rated || detail.userNo.isMyUid
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        background.layer.cornerRadius = background.bounds.height * 0.5
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileScoreFloatingView {
    @objc
    private func handlePan(_ ges: UIPanGestureRecognizer) {
        let location = ges.location(in: superview)
        
        switch ges.state {
        case .began:
            break
        case .changed:
            center = location
            break
        default:
            updatePosition(animated: true)
            break
        }
    }
}

extension LNProfileScoreFloatingView {
    private func updatePosition(animated: Bool) {
        guard let superview else { return }
        let movement = { [weak self] in
            guard let self else { return }
            
            let y = center.y.bounded(min: 160, max: superview.bounds.height - 160)
//            if center.x > superview.bounds.width * 0.5 {
                center = .init(x: superview.bounds.width - bounds.width * 0.5 - 8, y: y)
//            } else {
//                center = .init(x: bounds.width * 0.5 + 8, y: y)
//            }
        }
        if animated {
            UIView.animate(withDuration: 0.25, animations: movement)
        } else {
            movement()
        }
    }
    
    private func setupViews() {
        let ic = UIImageView()
        ic.image = .icProfileFillScore
        addSubview(ic)
        ic.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
            make.leading.greaterThanOrEqualToSuperview()
        }
        
        background.image = .primary_7
        background.layer.cornerRadius = 9.5
        background.clipsToBounds = true
        addSubview(background)
        background.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.bottom.equalToSuperview()
            make.top.equalTo(ic.snp.bottom).offset(-12)
            make.width.lessThanOrEqualTo(74)
        }
        
        let label = UILabel()
        label.text = .init(key: "A00227")
        label.font = .init(name: UIFont.boldFontName, size: 11)
        label.textColor = .text_1
        label.textAlignment = .center
        label.numberOfLines = 2
        background.addSubview(label)
        label.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview().inset(2)
            make.horizontalEdges.equalToSuperview().inset(5.5)
        }
    }
    
    private func setupGesture() {
        let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
        addGestureRecognizer(pan)
        
        onTap { [weak self] in
            guard let self else { return }
            guard let curDetail else { return }
            
            let panel = LNProfileStaringPanel()
            panel.updateAvatar(url: curDetail.avatar)
            panel.handler = { [weak self] score in
                guard let self else { return }
                LNGameMateManager.shared.scoreGameMate(uid: curDetail.userNo, score: Int(score))
                { [weak self] success in
                    guard let self else { return }
                    if success {
                        showToast(.init(key: "A00228"))
                        isHidden = true
                    }
                }
            }
            panel.popup()
        }
    }
}

#if DEBUG

import SwiftUI

struct LNProfileScoreFloatingViewPreview: UIViewRepresentable {
    func makeUIView(context: Context) -> some UIView {
        let container = UIView()
        container.backgroundColor = .lightGray
        
        let view = LNProfileScoreFloatingView()
        container.addSubview(view)
        view.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        return container
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) { }
}

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