//
//  TSThemeVC.swift
//  AIRingtone
//
//  Created by 100Years on 2025/2/27.
//

import MJRefresh
class TSThemeVC: TSBaseVC {

    lazy var viewModel:TSThemeVM = {
        let viewModel = TSThemeVM()
        viewModel.animationView = self.view
        return viewModel
    }()
    
    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()

        let titleImageView = UIImageView.createImageView(imageName: "nav_title_theme",contentMode: .scaleToFill)
        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(375)
            make.height.equalTo(48)
        }
        
        let tutorialsBtn = UIButton.createButton(image: UIImage(named: "tutorials")){[weak self]  in
            guard let self = self else { return }
            kPushVC(target: self, modelVC: TSThemeTutorialsVC())
        }
        navBarView.barView.addSubview(tutorialsBtn)
        tutorialsBtn.snp.makeConstraints { make in
            make.trailing.equalTo(-16)
            make.centerY.equalToSuperview()
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        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 indexPath.section == 0{
                kPushVC(target: self, modelVC: TSAIPhotoVC())
                return
            }
            
            if let sections = viewModel.colDataArray.safeObj(At: indexPath.section) as? TSColVVMSectionModel{
                let browseVC = TSThemeBrowseVC(currentIndex: indexPath.row, themeViewModel: viewModel) { [weak self]  in
                    guard let self = self else { return }
                    collectionComponent.reloadData()
                }
                kPushVC(target: self, modelVC: browseVC)
            }

        }
        
        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.reloadData()
                }
                collectionComponent.collectionView.endRefreshing()
            }
        }
        
        cp.collectionView.addLoadMore { [weak self]  in
            guard let self = self else { return }
            
            viewModel.getMoreData { [weak self] success,haveMore in
                guard let self = self else { return }
                if success {
                    collectionComponent.reloadData()
                }
                collectionComponent.collectionView.endRefreshing(noMore: !haveMore)
            }
        }

        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.getThemeData {[weak self] success in
            guard let self = self else { return }
            if success {
                collectionComponent.clear()
                collectionComponent.reloadView(with:viewModel.colDataArray)
            }
        }
    }

}

