//
//  WA_NewHomeVC.swift
//  wallpaper_project
//
//  Created by 忆海16 on 2024/3/28.
//

import UIKit
import AppLovinSDK
import FirebaseAnalytics
class WA_NewHomeVC: WA_RootVC {
    
    @IBOutlet weak var tableView: UITableView!
    
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setTableView()
        // 创建 ExampleViewController 实例
        
        
        // 添加 ExampleViewController 到当前视图控制器中
//        addChild(exampleViewController)
//        view.addSubview(exampleViewController.view)
//        exampleViewController.didMove(toParent: self)
        Analytics.logEvent("in_home", parameters: nil)
    }
    
    func setTableView(){
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: "WA_NewHometypeCell", bundle: nil), forCellReuseIdentifier: "WA_NewHometypeCell")
        tableView.register(UINib(nibName: "WA_NewHomeBannerCell", bundle: nil), forCellReuseIdentifier: "WA_NewHomeBannerCell")
        tableView.register(UINib(nibName: "WA_TodayCell", bundle: nil), forCellReuseIdentifier: "WA_TodayCell")
        tableView.register(UINib(nibName: "WA_MonRankngCell", bundle: nil), forCellReuseIdentifier: "WA_MonRankngCell")
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.isHidden = true
        
    }
    

    
}
extension WA_NewHomeVC:UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return 1
        }else if section == 1{
            return 1
        }else if section == 2{
            return 1
        }else{
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0{
            let cell = tableView.dequeueReusableCell(withIdentifier: "WA_NewHomeBannerCell", for: indexPath)as! WA_NewHomeBannerCell
            return cell
        }else if indexPath.section == 1{
            let cell = tableView.dequeueReusableCell(withIdentifier: "WA_NewHometypeCell", for: indexPath)as! WA_NewHometypeCell
            cell.liveBlock = {
                let vc = WA_LIVEVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            cell.jpgblock = { type in
                let vc = WA_4KVC()
                vc.type = type
                self.navigationController?.pushViewController(vc, animated: true)
                
            }
            return cell
        }else if indexPath.section == 2{
            let cell = tableView.dequeueReusableCell(withIdentifier: "WA_TodayCell", for: indexPath)as! WA_TodayCell
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "WA_MonRankngCell", for: indexPath)as! WA_MonRankngCell
            return cell
        }
    }
    
    
}
extension UIApplication {
    class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return getTopViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            if let selected = tab.selectedViewController {
                return getTopViewController(base: selected)
            }
        }
        if let presented = base?.presentedViewController {
            return getTopViewController(base: presented)
        }
        return base
    }
}


