//
//  FriendsRootHomeController.swift

//
//  Created by Leon on 2025/7/22.
//

import JXPagingView
import JXSegmentedView
import UIKit

class FriendsRootHomeController: CLTabRootController<FriendsRootHomeView> {
    private var naviMoreButton: EPIconGhostButton!

    private let segmentedViewHeight = 44

    private lazy var segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: CGFloat(segmentedViewHeight)))
    private lazy var listContainerView = JXSegmentedListContainerView(dataSource: self)

    private let dataSource = JXSegmentedTitleDataSource()
    private let titles = ["Message", "Friends"]

    lazy var messageListVc = SessionListController()
    lazy var friendsListVc = FrinendsListController()

    override func viewDidLoad() {
        super.viewDidLoad()

        // view.showEmpty(text: "Friends Coming soon")

        setupViews()
        setupDats()
        setupEvents()
    }

    private func setupViews() {
        navigationView.bgView.alpha = 0
        
        naviMoreButton = {
            let v = EPIconGhostButton(radius: .none, iconSize: .medium, iconCode: .chatroomMore)
            v.addTarget(self, action: #selector(tapMore(sender:)), for: .touchUpInside)
            navigationView.rightStackH.addArrangedSubview(v)
            v.snp.makeConstraints { make in
                make.size.equalTo(CGSize(width: 52, height: 44))
            }
            return v
        }()

        setupJXViews()
    }
    
    private func setupJXViews(){
        dataSource.naviTitleStyle()
        dataSource.titles = titles

        segmentedView.naviTitleStyle()
        segmentedView.dataSource = dataSource
        segmentedView.delegate = self

        navigationView.addSubview(segmentedView)
        segmentedView.snp.makeConstraints { make in
            make.height.equalTo(44)
            make.leading.equalToSuperview()
            make.bottom.equalToSuperview()
            make.trailing.equalTo(naviMoreButton.snp.leading).offset(-8)
        }

        listContainerView.contentScrollView().isScrollEnabled = false
        view.addSubview(listContainerView)
        listContainerView.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }

        segmentedView.listContainer = listContainerView
    }

    private func setupDats() {
    }

    private func setupEvents() {
    }

    // MARK: - Action

    @objc private func tapMore(sender: UIButton) {
        let pop = CLPopoverListView()
        let rect = view.convert(sender.frame, from: sender.superview)
        var items = [CLPopoverListTextItem]()
//        do {
//            let listItem = CLPopoverListTextItem()
//            listItem.title = "Notice"
//            listItem.image = MWIconFont.image(fromIcon: .messages, size: CGSize(width: 20, height: 20), color: .text)
//            listItem.updateLayout()
//            listItem.selectedHandler = { _ in
//                AppRouter.goNoticeCenter()
//            }
//            items.append(listItem)
//        }
        do {
            let listItem = CLPopoverListTextItem()
            listItem.title = "Search"
            listItem.image = MWIconFont.image(fromIcon: .search, size: CGSize(width: 20, height: 20), color: .text)
            listItem.updateLayout()
            listItem.selectedHandler = {[weak self] _ in
                let search = FriendMainSearchController()
                self?.presentNaviRootVc(vc: search)
            }
            items.append(listItem)
        }

        do {
            let listItem = CLPopoverListTextItem()
            listItem.title = "Read All"
            listItem.image = MWIconFont.image(fromIcon: .clear, size: CGSize(width: 20, height: 20), color: .text)
            listItem.updateLayout()
            listItem.selectedHandler = { _ in
                IMManager.shared.clearAllUnread()
            }
            items.append(listItem)
        }

        do {
            let listItem = CLPopoverListTextItem()
            listItem.title = "Delete All"
            listItem.image = MWIconFont.image(fromIcon: .iconDelete, size: CGSize(width: 20, height: 20), color: .text)
            listItem.updateLayout()
            listItem.selectedHandler = {[weak self] _ in
                let alert = Alert(title: "删除内容", text: "删除全部消息后，将清空所有的消息记录")
                let action1 = AlertAction(title: "删除", actionStyle: .confirm) {[weak self] in
                    self?.messageListVc.deleteAllConversations()
                }
                let action2 = AlertAction(title: "Cancel", actionStyle: .cancel)
                alert.addAction(action1)
                alert.addAction(action2)
                alert.show()
                
            }
            items.append(listItem)
        }

        pop.setupCommonPopover(rect, inView: view, items: items, block: nil)
    }
}

extension FriendsRootHomeController: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
    }
}

extension FriendsRootHomeController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return titles.count
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0 {
            return messageListVc
        } else {
            return friendsListVc
        }
    }
}
