//
//  LNIMChatUserMenuView.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/10.
//

import Foundation
import UIKit
import SnapKit
import Combine


class LNIMChatUserMenuView: LNPopupView {
    private let muteSwitch = UISwitch()
    private let blackLabel = UILabel()
    
    var viewModel: LNIMChatViewModel? {
        didSet {
            viewModel?.$messageOpt.sink { [weak self] opt in
                guard let self else { return }
                muteSwitch.isOn = opt == .RECEIVE_NOT_NOTIFY_MESSAGE
            }.store(in: &cancellables)
            blackLabel.text = viewModel?.userId.isInMyBlackList != true ? .init(key: "A00044") : .init(key: "A00045")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNIMChatUserMenuView {
    private func setupViews() {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 0
        container.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
        
        let menus = [
            buildMute(),
            
            buildLine(),
            buildRemark(),
            
            buildLine(),
            buildBlack(),
            
            buildLine(),
            buildReport()]
        menus.forEach {
            stackView.addArrangedSubview($0)
        }
    }
    
    private func buildMute() -> UIView {
        let scaleX: CGFloat = 40.0 / 51.0
        let scaleY: CGFloat = 24.5 / 31.0
        muteSwitch.onTintColor = .primary_5
        muteSwitch.transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
        muteSwitch.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            viewModel?.updateMessageOpt(opt: muteSwitch.isOn ? .RECEIVE_NOT_NOTIFY_MESSAGE : .RECEIVE_MESSAGE)
        }), for: .valueChanged)
        return buildMenuItem(icon: .icImChatMenuMute, title: .init(key: "A00085"), contentView: muteSwitch)
    }
    
    private func buildRemark() -> UIView {
        let menu = buildMenuItem(icon: .icImChatMenuRemark, title: .init(key: "A00086"), contentView: nil)
        menu.onTap { [weak self] in
            guard let self else { return }
            let panel = LNIMChatUserRemarkPanel()
            panel.update(viewModel?.remark ?? "")
            panel.handler = { [weak self] remark in
                guard let self else { return }
                dismiss()
                viewModel?.updateRemark(remark)
            }
            panel.popup()
        }
        
        return menu
    }
    
    private func buildBlack() -> UIView {
        blackLabel.font = .body_m
        blackLabel.textColor = .text_5
        let menu = buildMenuItem(icon: .icImChatMenuBlack, titleView: blackLabel, contentView: nil)
        
        menu.onTap { [weak self] in
            guard let self else { return }
            guard let uid = viewModel?.userId else { return }
            dismiss()
            if viewModel?.userId.isInMyBlackList != true {
                LNCommonAlertView.showBlackAlert(uid: uid)
            } else {
                LNRelationManager.shared.blackListUser(uid: uid, black: false, handler: nil)
            }
        }
        
        return menu
    }
    
    private func buildReport() -> UIView {
        let menu = buildMenuItem(icon: .icImChatMenuRemark, title: .init(key: "A00043"), contentView: nil)
        
        menu.onTap { [weak self] in
            guard let self else { return }
            guard let uid = viewModel?.userId else { return }
            dismiss()
            pushToReport(uid: uid)
        }
        
        return menu
    }
    
    private func buildMenuItem(icon: UIImage, title: String, contentView: UIView?) -> UIView {
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.font = .body_m
        titleLabel.textColor = .text_5
        
        return buildMenuItem(icon: icon, titleView: titleLabel, contentView: contentView)
    }
    
    private func buildMenuItem(icon: UIImage, titleView: UIView, contentView: UIView?) -> UIView {
        let container = UIView()
        container.snp.makeConstraints { make in
            make.height.equalTo(52)
        }
        
        let ic = UIImageView()
        ic.image = icon
        container.addSubview(ic)
        ic.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
        }
        
        container.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(ic.snp.trailing).offset(10)
        }
        
        if let contentView {
            container.addSubview(contentView)
            contentView.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.trailing.equalToSuperview().offset(-16)
            }
        } else {
            let arrow = UIImageView()
            arrow.image = .init(systemName: "chevron.forward")
            arrow.tintColor = .text_4
            container.addSubview(arrow)
            arrow.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.trailing.equalToSuperview().offset(-16)
            }
        }
        
        return container
    }
    
    private func buildLine() -> UIView {
        let container = UIView()
        
        let line = UIView()
        line.backgroundColor = .fill_2
        container.addSubview(line)
        line.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.height.equalTo(1)
            make.horizontalEdges.equalToSuperview().inset(16)
        }
        
        return container
    }
}
