//
//  DiscoverHeadLeaderBoardEntryView.swift

//
//  Created by Leon on 2025/9/9.
//
import JXSegmentedView
import UIKit
class DiscoverHeadLeaderBoardEntryView: UIView {
    var topBgView: UIImageView!
    var entryButton: EPIconTertiaryButton!
    var titleLabel: CLLabel!

    let segmentedViewHeight = 32
    lazy var segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: CGFloat(segmentedViewHeight)))
    private let dataSource = JXSegmentedTagStyleDataSource()

    var topThreeView: DiscoverTopThreeRank!
    
    var types = [DiscoverRankType.chat, DiscoverRankType.heartbeat,  DiscoverRankType.gift]
    
    var data: DiscoverHomeInfo?
    var selectIndex : Int = 0
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupData()
        setupEvent()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupViews() {
        backgroundColor = .c.cbdi
        cornerRadius = 16
        
        topBgView = {
            let v = UIImageView()
            v.image = UIImage(named: "discover_home_bg_rank")
            addSubview(v)
            v.snp.makeConstraints { make in
                make.top.leading.trailing.equalToSuperview()
                make.height.equalTo(v.snp.width).multipliedBy(200.0/393.0)
            }
            return v
        }()
        
        entryButton = {
            let v = EPIconTertiaryButton(radius: .round, iconSize: .small, iconCode: .arrowRight)
            v.addTarget(self, action: #selector(tapEntryButton), for: .touchUpInside)
            addSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 32, height: 32))
                make.top.equalToSuperview().offset(16)
                make.trailing.equalToSuperview().offset(-16)
            }
            return v
        }()

        titleLabel = {
            let v = CLLabel()
            v.font = .t.ttm
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.centerY.equalTo(entryButton)
            }
            return v
        }()

        do {
            // dataSource.titles = ["Chat", "Heartbeat", "Gift"]
            segmentedView.contentEdgeInsetLeft = 16
            segmentedView.contentEdgeInsetRight = 16
            addSubview(segmentedView)
            segmentedView.snp.makeConstraints { make in
                make.height.equalTo(32)
                make.leading.equalToSuperview()
                make.trailing.equalToSuperview()
                make.top.equalTo(entryButton.snp.bottom).offset(16)
            }
            segmentedView.dataSource = dataSource
            segmentedView.delegate = self
        }
        
        topThreeView = {
            let v = DiscoverTopThreeRank()
            addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.trailing.equalToSuperview().offset(-16)
                make.top.equalTo(segmentedView.snp.bottom).offset(16)
                make.bottom.equalToSuperview().offset(-16)
            }
            return v
        }()

        titleLabel.text = "Leaderboards"
    }

    private func setupData() {
        var titles = [String]()
        for per in types {
            titles.append(per.localizedText)
        }
        dataSource.titles = titles
//        dataSource.titles = ["Chat", "Heartbeat", "Gift"]
        segmentedView.reloadData()
    }

    private func setupEvent() {
    }
    
    // MARK: - Public
    
    func config(_ data: DiscoverHomeInfo?){
        self.data = data
        
        refreshRankData()
    }
    
    // MARK: - Helper
    
    func refreshRankData(){
        guard let info = data else{
            return
        }
        
        topThreeView.type = types[segmentedView.selectedIndex]
        
        if segmentedView.selectedIndex == 0{
            topThreeView.config(info.aiChatRankTop3List)
        }else if segmentedView.selectedIndex == 1{
            topThreeView.config(info.aiHeartbeatRankTop3List)
        }else if segmentedView.selectedIndex == 2{
            topThreeView.config(info.aiGiftRankTop3List)
        }
    }
    
    // MARK: - Action
    
    @objc private func tapEntryButton(){
        AppRouter.goLeaderboardPager()
    }
}

extension DiscoverHeadLeaderBoardEntryView: JXSegmentedViewDelegate{
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int){
        if selectIndex != index{
            selectIndex = index
            refreshRankData()
        }
    }
}
