//
//  LNGameFilterPanel.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/25.
//

import Foundation
import UIKit
import SnapKit
import Combine


protocol LNGameFilterPanelDelegate: AnyObject {
    func onGameFilterPanel(panel: LNGameFilterPanel, didSelectFilter filter: LNGameMateFilter, topType: LNGameTypeItemVO, category: LNGameCategoryItemVO)
}


class LNGameFilterPanel: LNPopupView {
    private let scrollView = UIScrollView()
    
    private let gradientLayer = CAGradientLayer()
    private var lastPosition: CGPoint = .zero
    
    private let backButton = UIButton()
    private let categoryPanel = LNGameCategoryFilterPanel()
    private let filterPanel = LNGameMateFilterPanel()
    
    private var curGameType: LNGameTypeItemVO?
    private var curGameCategory: LNGameCategoryItemVO?
    
    weak var delegate: LNGameFilterPanelDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        containerHeight = .height(512)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LNGameFilterPanel: LNGameCategoryFilterPanelDelegate {
    func onGameCategoryFilterPanel(panel: LNGameCategoryFilterPanel, didSelect game: LNGameCategoryItemVO, gameType: LNGameTypeItemVO) {
        curGameType = gameType
        curGameCategory = game
        
        filterPanel.update(game: game, filter: LNGameMateFilter())
        scrollView.setContentOffset(.init(x: scrollView.bounds.width, y: 0), animated: true)
        backButton.isHidden = false
    }
}

extension LNGameFilterPanel: LNGameMateFilterPanelDelegate {
    func onGameMateFilterPanel(panel: LNGameMateFilterPanel, filterGame filter: LNGameMateFilter) {
        dismiss()
        guard let curGameType, let curGameCategory else { return }
        delegate?.onGameFilterPanel(panel: self, didSelectFilter: filter, topType: curGameType, category: curGameCategory)
    }
}

extension LNGameFilterPanel {
    private func setupViews() {
        gradientLayer.colors = [
            UIColor.init(hex: "#B7EEFF").cgColor,
            UIColor.init(hex: "#FFFFFF").cgColor
        ]
        gradientLayer.cornerRadius = 20
        gradientLayer.locations = [0, 1]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 0, y: 1)
        gradientLayer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        container.layer.addSublayer(gradientLayer)
        
        container.publisher(for: \.bounds).removeDuplicates().sink { [weak self] newValue in
            guard let self else { return }
            gradientLayer.frame = container.bounds
        }.store(in: &cancellables)
        
        let titleLabel = UILabel()
        titleLabel.font = .heading_h3
        titleLabel.textColor = .text_5
        titleLabel.text = "Find your partner"
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(30)
        }
        
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.isScrollEnabled = false
        container.addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.bottom.equalToSuperview().offset(commonBottomInset)
        }
        
        let fakeView = UIView()
        scrollView.addSubview(fakeView)
        fakeView.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.height.equalToSuperview()
            make.width.equalTo(0)
        }
        
        let category = buildGameCategoryFilterView()
        scrollView.addSubview(category)
        category.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.leading.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        let filterPanel = buildFilterPanel()
        scrollView.addSubview(filterPanel)
        filterPanel.snp.makeConstraints { make in
            make.verticalEdges.equalToSuperview()
            make.width.equalToSuperview()
            make.trailing.equalToSuperview()
            make.leading.equalTo(category.snp.trailing)
        }
        
        let buttonImage: UIImage? = .init(systemName: "chevron.backward")?.withRenderingMode(.alwaysTemplate)
        backButton.contentHorizontalAlignment = .center
        backButton.setImage(buttonImage, for: .normal)
        backButton.tintColor = .black
        backButton.addAction(UIAction(handler: { [weak self] _ in
            guard let self else { return }
            scrollView.setContentOffset(.zero, animated: true)
            backButton.isHidden = true
        }), for: .touchUpInside)
        backButton.isHidden = true
        container.addSubview(backButton)
        backButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(14)
            make.leading.equalToSuperview().offset(16)
        }
    }
    
    private func buildGameCategoryFilterView() -> UIView {
        categoryPanel.delegate = self
        
        return categoryPanel
    }
    
    private func buildFilterPanel() -> UIView {
        filterPanel.delegate = self
        
        return filterPanel
    }
}
