CNLiveAuthViewController.swift 15.7 KB
//
//  CNLiveAuthViewController.swift
//  metaCode
//
//  Created by zx on 2023/12/28.
//

import Foundation
import MJRefresh
class CNLiveAuthViewController:CNLiveBaseViewController,UITableViewDelegate,UITableViewDataSource{
        
    //下滑tableview背景颜色
    let kCNDownSlideBackgroundColorHex: String = "#F7F8FA"
    
    ///CNliveDownSlideModel Type
    let SlideModelTypeRecommend4: String = "4"//4一排两个 推荐课程&推荐活动
    let SlideModelTypeTodayRecommend74: String = "74"//74今日推荐
    let SlideModelTypeAllCourse12: String = "12"//12列表全部课程
    let SlideModelTypeOneCourse75: String = "75"//75单课程大图
            
    let kCNLiveDownSlideAllCourseCellIdentifier = "kCNLiveDownSlideAllCourseCellIdentifier"
    let kCNLiveDownSlideRecommendCellIdentifier = "kCNLiveDownSlideRecommendCellIdentifier"
    let kCNLiveDownSlideTodayRecommendCellIdentifier = "kCNLiveDownSlideTodayRecommendCellIdentifier"
    let kCNLiveDownSlideSingleCourseCellIdentifier = "kCNLiveDownSlideSingleCourseCellIdentifier"
    let kCNLiveHomePageDuanJuCellIdentifier = "kCNLiveHomePageDuanJuCellIdentifier"
    
    var isHaveDuanJU = false//是否有在追短剧
    var totolNum = 0//一共显示多少行cell
    
    var selectedCell: CNDownSlideSingleCourseCell?
    var selectedDuanjuCell: CNLiveHomePageDuanJuCell?

    var statusBarShouldBeHidden = false
    //we need to set `View controller-based status bar appearance = YES` in info.plist.
    //so we can be able to hide statusBar.
    override var prefersStatusBarHidden: Bool {
        return statusBarShouldBeHidden
    }
    
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return .slide
    }
    
    // MARK: - 懒加载
    ///数据源
    lazy var dataArray: NSMutableArray = {
        var dataArray = NSMutableArray()
        return dataArray
    }()
    
    //短剧的数据源
    lazy var duanjuDataArray: NSMutableArray = {
        var dataArray = NSMutableArray()
        return dataArray
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.isHidden = false
        self.updateStatusBarAndTabbarFrame(visible: true)
        self.getMyAlbumCollection(page: 1)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        view.addSubview(tableView)
        tableView.frame = CGRect(x: 0, y: KStatusBarHeight, width: KSreenWidth, height: KSreenHeight-DownSlideBottomViewHeight-KStatusBarHeight)
        self.addRefreshView()
        self.getHomePageConfigListApi()
    }
    
    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        //        tableView.register(CNDownSlideSingleCourseCell.self, forCellReuseIdentifier: kCNLiveDownSlideCellIdentifier)
        tableView.register(CNLiveHomePageDuanJuCell.self, forCellReuseIdentifier: kCNLiveHomePageDuanJuCellIdentifier)
        
        tableView.register(CNLiveDownSlideRecommendCell.self, forCellReuseIdentifier: kCNLiveDownSlideRecommendCellIdentifier)
        tableView.register(CNLiveDownSlideTodayRecommendCell.self, forCellReuseIdentifier: kCNLiveDownSlideTodayRecommendCellIdentifier)
        tableView.register(CNDownSlideSingleCourseCell.self, forCellReuseIdentifier: kCNLiveDownSlideAllCourseCellIdentifier)
        tableView.register(CNDownSlideSingleCourseCell.self, forCellReuseIdentifier:kCNLiveDownSlideSingleCourseCellIdentifier)
        tableView.backgroundColor = HexColor(kCNDownSlideBackgroundColorHex)
        return tableView
    }()
    
    private func updateStatusBarAndTabbarFrame(visible: Bool) {
        statusBarShouldBeHidden = !visible
        UIView.animate(withDuration: 0.25) {
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    // MARK: - UITableViewDelegate
    func numberOfSections(in tableView: UITableView) -> Int {
        if self.isHaveDuanJU{
            return 2
        }else{
            return 1
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.isHaveDuanJU && section == 0{
            return 1
        }else{
            return self.dataArray.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if self.isHaveDuanJU && indexPath.section == 0{
            guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveHomePageDuanJuCellIdentifier) as? CNLiveHomePageDuanJuCell
            else {
                return CNLiveHomePageDuanJuCell()
            }
            
            cell.setModelArr(modelArr: self.duanjuDataArray)
            cell.DuanJUCellSelectedCallBack = {
                duanJuCell in
                self.selectedDuanjuCell = duanJuCell

                let detailVC = CNHomePageBaseNavigationController(rootViewController:CNHomepageDuanJuDetailViewController(cell: duanJuCell))
                
                detailVC.dismissClosure = { [weak self] in
                    guard let StrongSelf = self else { return }
                    StrongSelf.updateStatusBarAndTabbarFrame(visible: true)
                }
                self.updateStatusBarAndTabbarFrame(visible: false)
                self.present(detailVC, animated: true, completion: nil)
            }
            return cell
        }else{
            let slideModel = self.dataArray[indexPath.row] as! CNLiveDownSlideModel
            
            //type=4一排两个 推荐课程&推荐活动; 74今日推荐; 12列表全部课程; 75单课程大图
            if slideModel.type == SlideModelTypeRecommend4{
                guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideRecommendCellIdentifier) as? CNLiveDownSlideRecommendCell
                else {
                    return CNLiveDownSlideRecommendCell()
                }
                cell.slideModel = slideModel
                return cell
            }else if slideModel.type == SlideModelTypeTodayRecommend74{
                guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideTodayRecommendCellIdentifier) as? CNLiveDownSlideTodayRecommendCell
                else {
                    let cell = CNLiveDownSlideTodayRecommendCell()
                    return cell
                }
                cell.slideModel = slideModel
                return cell
            }else if slideModel.type == SlideModelTypeAllCourse12{
                guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideAllCourseCellIdentifier) as? CNDownSlideSingleCourseCell
                else {
                    let cell = CNDownSlideSingleCourseCell()
                    return cell
                }
                cell.slideModel = slideModel
                return cell
            }else if slideModel.type == SlideModelTypeOneCourse75{
                guard let cell = tableView.dequeueReusableCell(withIdentifier: kCNLiveDownSlideSingleCourseCellIdentifier) as? CNDownSlideSingleCourseCell
                else {
                    let cell = CNDownSlideSingleCourseCell()
                    return cell
                }
                cell.slideModel = slideModel
                return cell
            }else{
                return UITableViewCell()
            }
        }
    }
    
    
    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
        if self.isHaveDuanJU && indexPath.section == 0{

        }else{
            
            guard let row = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
            UIView.animate(withDuration: 0.1) {
                row.bgBackView.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
        if self.isHaveDuanJU && indexPath.section == 0{

        }else{
            
            guard let row = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
            UIView.animate(withDuration: 0.3) {
                row.bgBackView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.isHaveDuanJU && indexPath.section == 0{
            //点击在追短剧
            guard let cell = tableView.cellForRow(at: indexPath) as? CNLiveHomePageDuanJuCell else { return }
            selectedDuanjuCell = cell
            
            let detailVC = CNHomePageBaseNavigationController(rootViewController:CNHomepageDuanJuDetailViewController(cell: selectedDuanjuCell!))
            
            detailVC.dismissClosure = { [weak self] in
                guard let StrongSelf = self else { return }
                StrongSelf.updateStatusBarAndTabbarFrame(visible: true)
            }
            updateStatusBarAndTabbarFrame(visible: false)
            present(detailVC, animated: true, completion: nil)

        }else{
            
            let slideModel = self.dataArray[indexPath.row] as! CNLiveDownSlideModel
            var cellType:CNLiveHomePageCellType = .course
            ///model
            ///(1) 1,2时跳转:sharelUrl后面拼接 ?contentId=contentId&videoId=pid
            ///(2) 3,4时跳转:thirdUrl
            ///(3)“album”,“album_video”,“album_ugc” -> http://wjjh5.cnlive.com/cnYmAlbumVideoList.html?aid=contentId&title=titlle
            if slideModel.contents.first?.model ?? "0" == "1" || slideModel.contents.first?.model ?? "0" == "2" || slideModel.contents.first?.model ?? "0" == "3" || slideModel.contents.first?.model ?? "0" == "4"{
                cellType = .playletToCourse
            }else if slideModel.contents.first?.model ?? "0" == "album_ugc"{
                cellType = .playlet
            }else{
                //新类型
            }

            //type=4一排两个 推荐课程&推荐活动; 74今日推荐; 12列表全部课程; 75单课程大图
            if slideModel.type == SlideModelTypeRecommend4{
                
            }else if slideModel.type == SlideModelTypeTodayRecommend74{
                
            }else if slideModel.type == SlideModelTypeAllCourse12{
                guard let cell = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
                selectedCell = cell

                let detailVC = CNSlideBaseNavigationController(rootViewController: CNUpSlideDetailViewController(cell: cell,slideModel: slideModel, cellType: cellType), cellType: cellType)
                detailVC.dismissClosure = { [weak self] in
                    guard let StrongSelf = self else { return }
                    StrongSelf.updateStatusBarAndTabbarFrame(visible: true)
                }
                updateStatusBarAndTabbarFrame(visible: false)
                
                present(detailVC, animated: true, completion: nil)
            }else if slideModel.type == SlideModelTypeOneCourse75{
                guard let cell = tableView.cellForRow(at: indexPath) as? CNDownSlideSingleCourseCell else { return }
                selectedCell = cell
                let detailViewC = CNUpSlideDetailViewController(cell: cell,slideModel: slideModel, cellType: cellType) 
                let detailVC = CNSlideBaseNavigationController(rootViewController: detailViewC, cellType: cellType)
                
                detailVC.dismissClosure = { [weak self] in
                    guard let StrongSelf = self else { return }
                    StrongSelf.updateStatusBarAndTabbarFrame(visible: true)
                }
                updateStatusBarAndTabbarFrame(visible: false)
                present(detailVC, animated: true, completion: nil)
            }
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if self.isHaveDuanJU && indexPath.section == 0{
            if self.duanjuDataArray.count > 4{
                //标题55+4个cell*(45+15*2)+首尾15+12圆角
                return CGFloat(4)*(45+15*2)+CGFloat(55)+15+15+12

            }else{
                //标题55+n个cell*(45+15*2)+首尾15+12圆角
                return CGFloat(self.duanjuDataArray.count)*(45+15*2)+CGFloat(55)+15+15+12

            }
        }else{
            let slideModel = self.dataArray[indexPath.row] as! CNLiveDownSlideModel
            return CGFloat(slideModel.cellH)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func addRefreshView() {
        tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[weak self] in
            self!.getHomePageConfigListApi()
        })
    }
    //MARK: - 获取首页配置
    func getHomePageConfigListApi(){
        CNLiveHomePageViewModel.requestGetHomePageConfigList{ result, status in
            if status == .success{
                let dataModel = result as! CNLiveHomePageModel
                let homePageListMdls = dataModel.list
                for listModel in homePageListMdls {
                    if listModel.type == 3{
                        //在追短剧
                        self.getMyAlbumCollection(page: 1)
                    }else if listModel.type == 1{
                        //页面
                        self.getPageApi(pageID: listModel.content)
                    }
                }
            }else{
                self.getHomePageConfigListApi()
            }
        }
    }
    
    //我的专辑收藏--在追短剧
    func getMyAlbumCollection(page:NSInteger){
        CNLiveHomePageViewModel.requestGetHomePageMyAlbum(page: page){ result, status in
            if status == .success{
                let arr = result as! Array<CNLiveAlbumMsgModel>
                if arr.count>0{
                    //显示在追短剧cell
                    self.isHaveDuanJU = true
                    
                    self.duanjuDataArray = NSMutableArray(array: arr)
                }else{
                    //隐藏在追短剧cell
                    self.isHaveDuanJU = false
                }
                self.tableView.mj_header?.endRefreshing()
                self.tableView.reloadData()

            }else{
                
            }
        }
    }
    
    //MARK: - 网络请求获取区块详情页
    func getPageApi(pageID:String)
    {
        //        showLoading(message: "")
        CNLiveDownSlideViewModel.requestGetPage(pageId: pageID as NSString) { result, status in
            hiddenLoading()
            if status == .success{
                self.dataArray.removeAllObjects()
                let array = result as! [Any]
                let newDataArray = NSMutableArray()
                for slideModel in array {
                    if let model = slideModel as? CNLiveDownSlideModel{
                        
                        let layout = CNLiveDownSlideModelLayout.init(model: model)
                        newDataArray.addObjects(from: [layout.getCellHModel()])
                    }
                }
                self.dataArray.addObjects(from: newDataArray as! [Any])
                self.tableView.mj_header?.endRefreshing()
                self.tableView.reloadData()
            }else if (status == .failed){
                //                self.NetApiStatus = .apiError
            }else if (status == .noNetwork){
                
                //                self.NetApiStatus = .notNetwork
            }
        }
        //
    }
}