//
//  MusicContainerViewController.swift
//  DiaryWallPaper
//
//  Created by 倪锴伦 on 2025/1/20.
//

import ADManager
import BetterSegmentedControl
import Foundation
import TSVideoKit

class MusicHomeContainerViewController: LWBGViewController {
    lazy var navBar: LWRightNavigationBar = LWRightNavigationBar()
    lazy var searchBar: MusicSearchBar = MusicSearchBar()
    weak var importMenuView: BubbleMenuView?

    lazy var importButton: UIButton = {
        let bt = UIButton(frame: CGRect(x: 0, y: 0, width: 42, height: 42))
        bt.setImage(.icNavImport, for: .normal)
        return bt
    }()

    lazy var songlistVc: SongListViewController = SongListViewController()
    
    var viewModel: MusicContainerViewModel = MusicContainerViewModel()
    
    lazy var guideBubble: GuideBubbleView = {
        let guide = GuideBubbleView()
        guide.addTarget(self, action: #selector(dismissGuideBubble), for: .touchUpInside)
        guide.tapArea.addTarget(self, action: #selector(bubbleClick), for: .touchUpInside)
        return guide
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        bgImageView.image = .viewMainBg
        addTargets()
    }

    override func addNotifaction() {
        super.addNotifaction()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        PlayerManager.shared.rootVc?.moveUpMiniBar()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        print("TSConfiguration.isYs ===", TSConfiguration.isYs)
        if UserDefaults.standard.string(forKey: "GuideKey") == nil, TSConfiguration.isYs {
            view.addSubview(guideBubble)
            view.bringSubviewToFront(guideBubble)
            guideBubble.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
    }

    func addTargets() {
        searchBar.addTarget(self, action: #selector(showSearchViewController), for: .touchUpInside)
        importButton.addTarget(self, action: #selector(showImportMenuView), for: .touchUpInside)
    }

    @objc func showImportMenuView() {
        let menuView = BubbleMenuView()
        menuView.position = FitManager.isAr ? .topLeft : .topRight
        menuView.actionHanlder = { type in
            self.menuClick(type: type)
        }
        view.addSubview(menuView)
        menuView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.top.equalTo(self.importButton.snp.bottom).offset(0)
        }
        importMenuView = menuView
    }

    func menuClick(type: ImportSource) {
        importMenuView?.removeFromSuperview()
        if type == .library {
            ImportFilesManager.shared.openPhotoLibrary(parent: self)
        } else if type == .file {
            ImportFilesManager.shared.openFileDocument(parent: self, completion: nil)
        }
    }

    @objc func bubbleClick() {
        showSearchViewController()
        dismissGuideBubble()
    }

    @objc func dismissGuideBubble() {
        guideBubble.isHidden = true
        UserDefaults.standard.setValue("Guide", forKey: "GuideKey")
    }

    @objc func showSearchViewController() {
        if PurchaseManager.default.isVip {
            if TSConfiguration.isYs {
                let vc = SearchOnlineViewController()
                vc.hidesBottomBarWhenPushed = true
                navigationController?.pushViewController(vc, animated: true)
            } else {
                let vc = LocalSearchViewController()
                vc.hidesBottomBarWhenPushed = true
                navigationController?.pushViewController(vc, animated: true)
            }
        } else {
            ADManager.shared.showAd(scene: ADScene.searchInsert, from: self) { state in
                if state == .finished || state == .fail {
                    if TSConfiguration.isYs {
                        let vc = SearchOnlineViewController()
                        vc.hidesBottomBarWhenPushed = true
                        self.navigationController?.pushViewController(vc, animated: true)
                    } else {
                        let vc = LocalSearchViewController()
                        vc.hidesBottomBarWhenPushed = true
                        self.navigationController?.pushViewController(vc, animated: true)
                    }
                }
            }
        }
    }

    override func addChildren() {
        super.addChildren()
        view.addSubview(navBar)
        navBar.appendRightItem(item: importButton)
        view.addSubview(searchBar)
        view.addSubview(songlistVc.view)
        addChild(songlistVc)
        songlistVc.didMove(toParent: self)
    }

    override func makeConstarints() {
        super.makeConstarints()
        navBar.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.horizontalEdges.equalToSuperview()
            make.height.equalTo(k_Height_NavBar)
        }
        searchBar.snp.makeConstraints { make in
            make.top.equalTo(navBar.snp.bottom)
            make.horizontalEdges.equalToSuperview().inset(16)
            make.height.equalTo(44)
        }

        songlistVc.view.snp.makeConstraints { make in
            make.top.equalTo(searchBar.snp.bottom).offset(8)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.left.right.equalToSuperview()
        }
    }
}
