//
//  LNGameMateListMenuView.swift
//  Lanu
//
//  Created by OneeChan on 2025/11/14.
//

import Foundation
import UIKit
import SnapKit


protocol LNGameMateListMenuViewDelegate: AnyObject {
    func menuView(view: LNGameMateListMenuView, scoreTypeChanged newType: LNSortedType)
    func menuView(view: LNGameMateListMenuView, priceTypeChanged newType: LNSortedType)
    func menuViewDidClickFind(view: LNGameMateListMenuView)
}

class LNGameMateListMenuView: UIView {
    private let priceSortView = LNSortedEditView()
    private let scoreSortView = LNSortedEditView()
    
    weak var delegate: LNGameMateListMenuViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNGameMateListMenuView: LNSortedEditViewDelegate {
    func sortedEditView(view: LNSortedEditView, sortedTypeChanged: LNSortedType) {
        if view == priceSortView {
            delegate?.menuView(view: self, priceTypeChanged: sortedTypeChanged)
        } else if view == scoreSortView {
            delegate?.menuView(view: self, scoreTypeChanged: sortedTypeChanged)
        }
    }
}

extension LNGameMateListMenuView {
    private func setupViews() {
        scoreSortView.titleLabel.text = .init(key: "A00034")
        scoreSortView.delegate = self
        addSubview(scoreSortView)
        scoreSortView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        priceSortView.titleLabel.text = .init(key: "A00035")
        priceSortView.delegate = self
        addSubview(priceSortView)
        priceSortView.snp.makeConstraints { make in
            make.leading.equalTo(scoreSortView.snp.trailing).offset(7)
            make.centerY.equalToSuperview()
        }
        
        let findButton = UIButton()
        findButton.setBackgroundImage(.icGameMateListMenuFind, for: .normal)
        findButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            self.delegate?.menuViewDidClickFind(view: self)
        }), for: .touchUpInside)
        addSubview(findButton)
        findButton.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.trailing.equalToSuperview()
        }
        
        let find = UIView()
        find.isUserInteractionEnabled = false
        findButton.addSubview(find)
        find.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(11)
            make.trailing.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
        }
        
        let findLabel = UILabel()
        findLabel.text = .init(key: "A00038")
        findLabel.font = .heading_h4
        findLabel.textColor = .text_4
        find.addSubview(findLabel)
        findLabel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalToSuperview()
        }
        
        let arrow = UIImageView.arrowImageView(size: 14)
        arrow.tintColor = .text_3
        find.addSubview(arrow)
        arrow.snp.makeConstraints { make in
            make.centerY.trailing.equalToSuperview()
            make.leading.greaterThanOrEqualTo(findLabel.snp.trailing).offset(3)
        }
    }
}


#if DEBUG

import SwiftUI

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

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

