//
//  TSDiscoverVC.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/17.
//

class TSDiscoverVC: TSBaseVC {
    lazy var viewModel:TSDiscoverVM = {
        let viewModel = TSDiscoverVM()
        viewModel.animationView = self.view
        return viewModel
    }()
    
    lazy var redDot: UIView = {
        let redDot = UIView()
        redDot.backgroundColor = .themeColor
        redDot.cornerRadius = 3
        redDot.isHidden = true
        return redDot
    }()
    
    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()

        let titleImageView = UIImageView.createImageView(imageName: "nav_title_discover",contentMode: .scaleToFill)
        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(375)
            make.height.equalTo(48)
        }
        
        let rightBtn = UIButton.createButton(image: UIImage(named: "ring_folder")){[weak self]  in
            guard let self = self else { return }
            kPushVC(target: self, modelVC: TSRingDownVC())
        }
        navBarView.barView.addSubview(rightBtn)
        rightBtn.snp.makeConstraints { make in
            make.trailing.equalTo(-16)
            make.centerY.equalToSuperview()
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        rightBtn.addSubview(redDot)
        redDot.snp.makeConstraints { make in
            make.trailing.equalTo(2)
            make.top.equalTo(1)
            make.width.equalTo(6)
            make.height.equalTo(6)
        }
        
        
        
        return navBarView
    }()
    
    
    lazy var collectionComponent: TSCollectionViewComponent = {
        let layout = UICollectionViewFlowLayout()
        let cp = TSCollectionViewComponent(frame: CGRect.zero, layout: layout, attributes: [ :])
        cp.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
        
        cp.itemDidSelectedHandler = { [weak self] (object, indexPath) in
            guard let self = self else { return }
            if let sections = viewModel.colDataArray.safeObj(At: indexPath.section) as? TSColVVMSectionModel,
               let itemModel = sections.items.safeObj(At: indexPath.row),
                let ringCategoryModel = itemModel.dataModel as? TSRingCategoryModel{
                kPushVC(target: self, modelVC: TSDiscoverListVC(ringCategoryModel: ringCategoryModel))
            }
        }
        
        cp.collectionView.addRefresh{ [weak self]  in
            guard let self = self else { return }
            viewModel.reloadAllData { [weak self] success  in
                guard let self = self else { return }
                if success {
                    collectionComponent.clear()
                    collectionComponent.reloadView(with:viewModel.colDataArray)
                }
                collectionComponent.collectionView.endRefreshing()
            }
        }
        
        return cp
    }()
    
    
    
    override func createView() {
        
        setViewBgImageNamed(named: kViewBJ)

        edgesForExtendedLayout = []
        navBarContentView.addSubview(navBarView)
        navBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(collectionComponent.collectionView)
        collectionComponent.collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override func dealThings() {
        viewModel.getData {[weak self] success in
            guard let self = self else { return }
            if success {
                collectionComponent.clear()
                collectionComponent.reloadView(with:viewModel.colDataArray)
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        redDot.isHidden = !TSMineRintoneHistory.isHaveNew
    }
}
