//
//  TSHomeVC.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

import TYCyclePagerView

import Photos
import PhotosUI

class TSHomeVC: TSBaseVC {
    lazy var navBarView: TSBaseNavContentBarView = {
        let navBarView = TSBaseNavContentBarView()
        let titleImageView = UIImageView.createImageView(imageName: "nav_title_home", contentMode: .scaleToFill)

        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
        }

        return navBarView
    }()

    lazy var hStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.distribution = .fillEqually
        stack.spacing = 16
        return stack
    }()

    lazy var diyButton: SpacedButton = {
        let btn = SpacedButton()
        btn.setTitle("DIY Live Photo", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setImage(UIImage(named: "top_diy"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "WallpaperBg"), for: .normal)
        btn.addTarget(self, action: #selector(showDiyPaperController), for: .touchUpInside)
        btn.titleLabel?.font = .systemFont14
        return btn
    }()

    lazy var shuffleButton: SpacedButton = {
        let btn = SpacedButton()
        btn.setTitle("Shuffle Packs", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setImage(UIImage(named: "top_shuffle"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "WallpaperBg"), for: .normal)
        btn.addTarget(self, action: #selector(showRandomWallpaperVC), for: .touchUpInside)
        btn.titleLabel?.font = .systemFont14
        return btn
    }()

    lazy var collectionComponent: CollectionViewComponent = {
        let layout = UICollectionViewFlowLayout()
        let cp = CollectionViewComponent(frame: CGRect.zero, layout: layout, attributes: [:])
        cp.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: k_Height_TabBar, right: 0)
        cp.itemDidSelectedHandler = { [weak self] _, indexPath in
            guard let self = self else { return }

            let obj = dataArray.safeObj(At: indexPath.section)
            if let bannerModel = obj as? TSHomeBannerDataSectionModel {
                if let items = bannerModel.itemModels.first {
                    kPresentModalVC(target: self, modelVC: TSLiveWallpaperBrowseVC(itemModels: items.items, currentIndex: indexPath.row))
                }
            } else if let liveModel = obj as? TSImageDataSectionModel {
                kPresentModalVC(target: self, modelVC: TSLiveWallpaperBrowseVC(itemModels: liveModel.items, currentIndex: indexPath.row))
            }
        }

        return cp
    }()

    var dataArray: [Component] = kImageDataCenterShared.liveBannerArray + kImageDataCenterShared.liveListArray
    override func createView() {
        setViewBgImageNamed(named: "view_main_bg")

        navBarContentView.addSubview(navBarView)
        navBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        hStack.addArrangedSubview(diyButton)
        hStack.addArrangedSubview(shuffleButton)
        contentView.addSubview(hStack)
        hStack.snp.makeConstraints { make in
            make.horizontalEdges.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(16)
            make.height.equalTo(60)
        }

        contentView.addSubview(collectionComponent.collectionView)
        collectionComponent.collectionView.snp.makeConstraints { make in
            make.top.equalTo(diyButton.snp.bottom)
            make.leading.trailing.bottom.equalToSuperview()
        }

        collectionComponent.reloadView(with: dataArray)

        TSNetworkShard.monitorNetworkPermission { success in
            if success {
                self.collectionComponent.reloadData()
            }
        }
    }

    @objc func showDiyPaperController() {
        let vc = TSEditLiveVC()
        vc.modalPresentationStyle = .overFullScreen
        present(vc, animated: true)
    }
    
    @objc func showRandomWallpaperVC() {
        let vc = TSRandomWallpaperVC()
        let nav = TSBaseNavigationC(rootViewController: vc)
        nav.modalPresentationStyle = .overFullScreen
        present(nav, animated: true)
    }
    
    
}
