//
//  LNRoomProfileBottomMenu.swift
//  Gami
//
//  Created by OneeChan on 2026/3/16.
//

import Foundation
import UIKit
import SnapKit


class LNRoomProfileBottomMenu: UIView {
    private let follow = UIButton()
    private let chat = UIButton()
    private let gift = UIButton()
    
    private var curUid: String?
    private var isFollow: Bool = false {
        didSet {
            if isFollow {
                follow.setTitle(.init(key: "A00026"), for: .normal)
                follow.setTitleColor(.text_1.withAlphaComponent(0.2), for: .normal)
            } else {
                follow.setTitle(.init(key: "A00225"), for: .normal)
                follow.setTitleColor(.text_1, for: .normal)
            }
        }
    }
    weak var panel: LNPopupView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.alignment = .fill
        stackView.distribution = .fillEqually
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview().offset(9)
            make.height.equalTo(48)
            make.bottom.equalToSuperview()
        }
        
        follow.setTitle(.init(key: "A00225"), for: .normal)
        follow.setTitleColor(.text_1, for: .normal)
        follow.titleLabel?.font = .heading_h3
        follow.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curUid else { return }
            if isFollow {
                LNCommonAlertView.showUnfollowAlert(uid: curUid)
            } else {
                LNRelationManager.shared.operateFollow(uid: curUid, follow: true, handler: nil)
            }
        }), for: .touchUpInside)
        stackView.addArrangedSubview(follow)
        addSeperator(follow)
        
        chat.setTitle(.init(key: "A00042"), for: .normal)
        chat.setTitleColor(.text_1, for: .normal)
        chat.titleLabel?.font = .heading_h3
        chat.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curUid else { return }
            panel?.dismiss()
            pushToChat(uid: curUid)
        }), for: .touchUpInside)
        stackView.addArrangedSubview(chat)
        addSeperator(chat)
        
        gift.setTitle(.init(key: "A00336"), for: .normal)
        gift.setTitleColor(.text_6, for: .normal)
        gift.titleLabel?.font = .heading_h3
        gift.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curUid else { return }
            panel?.dismiss()
            
            let panel = LNRoomGiftPanel()
            panel.update(LNRoomManager.shared.curRoom, selectedUid: curUid)
            panel.popup(self)
        }), for: .touchUpInside)
        stackView.addArrangedSubview(gift)
        
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ uid: String) {
        curUid = uid
        
        if uid.isMyUid {
            isHidden = true
            return
        }
        LNRelationManager.shared.getRelationWithUser(uid: uid, handler: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSeperator(_ view: UIView) {
        let sep = UIView()
        sep.backgroundColor = .text_3
        view.addSubview(sep)
        sep.snp.makeConstraints { make in
            make.width.equalTo(1)
            make.height.equalTo(12)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
        }
    }
}

extension LNRoomProfileBottomMenu: LNRelationManagerNotify {
    func onUserRelationChanged(uid: String, relation: LNUserRelationShip) {
        guard uid == curUid else { return }
        isFollow = relation.contains(.followed)
    }
}
