//
//  LNProfileBottomMenu.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/15.
//

import Foundation
import UIKit
import SnapKit
import Combine


class LNProfileBottomMenu: UIView {
    private let visitorMenu = UIStackView()
    private let followButton = UIButton()
    private let chatButton = UIButton()
    
    private let postFeedButton = UIButton()
    
    private let chatAnimateCover = UIImageView()
    
    private var curDetail: LNUserProfileVO?
    private var scene: LNCommonSceneType = .unknown
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        
        LNEventDeliver.addObserver(self)
    }
    
    func update(_ detail: LNUserProfileVO, scene: LNCommonSceneType) {
        visitorMenu.isHidden = detail.userNo.isMyUid
        postFeedButton.isHidden = !detail.userNo.isMyUid
        
        curDetail = detail
        self.scene = scene
        
        updateFollow(follow: detail.follow, animated: false)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNProfileBottomMenu: LNRelationManagerNotify {
    func onUserRelationChanged(uid: String, relation: LNUserRelationShip) {
        guard curDetail?.userNo == uid else { return }
        updateFollow(follow: relation.contains(.followed), animated: true)
    }
}

extension LNProfileBottomMenu {
    private func updateFollow(follow: Bool, animated: Bool) {
        if !animated {
            chatAnimateCover.alpha = 0.0
            if follow {
                followButton.isHidden = true
                chatButton.setBackgroundImage(.primary_8, for: .normal)
            } else {
                followButton.isHidden = false
                chatButton.setBackgroundImage(.primary_3, for: .normal)
            }
        } else {
            chatAnimateCover.alpha = 0.0
            UIView.animate(withDuration: 0.25) { [weak self] in
                guard let self else { return }
                if follow {
                    followButton.isHidden = true
                    chatAnimateCover.alpha = 1.0
                } else {
                    followButton.isHidden = false
                    chatButton.setBackgroundImage(.primary_3, for: .normal)
                }
            } completion: { [weak self] _ in
                guard let self else { return }
                if follow {
                    chatButton.setBackgroundImage(.primary_8, for: .normal)
                    chatAnimateCover.alpha = 0.0
                }
            }
        }
    }
    
    private func setupViews() {
        let bottomGradient = CAGradientLayer()
        bottomGradient.colors = [
            UIColor.white.withAlphaComponent(0).cgColor,
            UIColor.white.cgColor,
            UIColor.white.cgColor
        ]
        bottomGradient.locations = [0, 0.5, 1]
        bottomGradient.startPoint = .init(x: 0, y: 0)
        bottomGradient.endPoint = .init(x: 0, y: 1)
        layer.addSublayer(bottomGradient)
        publisher(for: \.bounds).removeDuplicates().sink { [weak bottomGradient] newValue in
            guard let bottomGradient else { return }
            bottomGradient.frame = newValue
        }.store(in: &cancellables)
        
        visitorMenu.axis = .horizontal
        visitorMenu.spacing = 10
        addSubview(visitorMenu)
        visitorMenu.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
        
        let follow = buildFollowView()
        visitorMenu.addArrangedSubview(follow)
        
        let chat = buildChatView()
        visitorMenu.addArrangedSubview(chat)
        
        let postView = buildSendPost()
        addSubview(postView)
        postView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
    }
    
    private func buildFollowView() -> UIView {
        followButton.layer.cornerRadius = 23.5
        followButton.setBackgroundImage(.primary_8, for: .normal)
        followButton.clipsToBounds = true
        followButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curDetail, !curDetail.userNo.isMyUid else { return }
            LNRelationManager.shared.operateFollow(uid: curDetail.userNo, follow: true, handler: nil)
        }), for: .touchUpInside)
        followButton.snp.makeConstraints { make in
            make.height.equalTo(47)
        }
        
        let followIc = UIImageView()
        followIc.image = .icProfileFollow
        followButton.addSubview(followIc)
        followIc.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(24)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(22)
        }
        
        let followLabel = UILabel()
        followLabel.text = .init(key: "A00225")
        followLabel.font = .heading_h3
        followLabel.textColor = .text_1
        followLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        followLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        followButton.addSubview(followLabel)
        followLabel.snp.makeConstraints { make in
            make.leading.equalTo(followIc.snp.trailing).offset(4)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-24)
        }
        
        return followButton
    }
    
    private func buildChatView() -> UIView {
        chatButton.setBackgroundImage(.primary_3, for: .normal)
        chatButton.layer.cornerRadius = 23.5
        chatButton.clipsToBounds = true
        chatButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            guard let curDetail else { return }
            pushToChat(uid: curDetail.userNo, scene: scene)
            LNStatisticManager.shared.reportClickChat(uid: curDetail.userNo)
        }), for: .touchUpInside)
        chatButton.snp.makeConstraints { make in
            make.height.equalTo(47)
        }
        
        chatAnimateCover.image = .primary_8
        chatButton.addSubview(chatAnimateCover)
        chatAnimateCover.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let container = UIView()
        container.isUserInteractionEnabled = false
        chatButton.addSubview(container)
        container.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let faceIc = UIImageView()
        faceIc.image = .icSkillChatFace
        container.addSubview(faceIc)
        faceIc.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        let chatLabel = UILabel()
        chatLabel.text = .init(key: "A00042")
        chatLabel.font = .heading_h3
        chatLabel.textColor = .text_1
        container.addSubview(chatLabel)
        chatLabel.snp.makeConstraints { make in
            make.leading.equalTo(faceIc.snp.trailing).offset(4)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        return chatButton
    }
    
    private func buildSendPost() -> UIView {
        postFeedButton.setBackgroundImage(.primary_8, for: .normal)
        postFeedButton.layer.cornerRadius = 23.5
        postFeedButton.clipsToBounds = true
        postFeedButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            pushToCreateFeed()
        }), for: .touchUpInside)
        postFeedButton.snp.makeConstraints { make in
            make.height.equalTo(47)
        }
        
        let container = UIView()
        container.isUserInteractionEnabled = false
        postFeedButton.addSubview(container)
        container.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let sendIc = UIImageView()
        sendIc.image = .icSend1
        container.addSubview(sendIc)
        sendIc.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalToSuperview()
        }
        
        let postLabel = UILabel()
        postLabel.text = .init(key: "A00294")
        postLabel.font = .heading_h3
        postLabel.textColor = .text_1
        container.addSubview(postLabel)
        postLabel.snp.makeConstraints { make in
            make.leading.equalTo(sendIc.snp.trailing).offset(4)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        return postFeedButton
    }
}
