//
//  YHMyLikeViewController.swift
//  galaxy
//
//  Created by EDY on 2024/4/11.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHMyLikeViewController: YHBaseViewController {
    let viewModel: YHMyLikeViewModel = YHMyLikeViewModel()
    var tableFootView: YHServiceTableFootView!
    var id = 1
    
    lazy var noDataView: YHEmptyDataView = {
        let view = YHEmptyDataView.createView("暂无数据", kEmptyCommonBgName)
        view.isHidden = true
        return view
    }()
    
    var dataSouce: [YHHomeListModel?] = [] {
        didSet {
            self.tableFootView.items = dataSouce
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        var title = "我的收藏"
        if id == 1 {
            title = "我的点赞"
        } else if id == 2{
            title = "我的收藏"
        } else {
            title = "浏览记录"
        }
        gk_navTitle = title
        setupUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
}

extension YHMyLikeViewController {
    func loadData() {
        if id == 1 || id == 2 {
            self.viewModel.getList(true ,id) {[weak self] success, error in
                guard let self = self else { return }
                if self.viewModel.arrHomeNewsData?.count == 0 {
                    self.tableFootView.isHidden = true
                    self.noDataView.isHidden = false
                } else {
                    self.tableFootView.isHidden = false
                    self.noDataView.isHidden = true
                }
                self.dataSouce = self.viewModel.arrHomeNewsData ?? []
                if self.viewModel.hasMoreForHomeNews == false {
                    self.tableFootView.myCollectView.es.noticeNoMoreData()
                    self.tableFootView.myCollectView.footer?.alpha = 1
                }
            }
        } else {
            self.viewModel.getList(true, callBackBlock: {[weak self] success, error in
                guard let self = self else { return }
                if self.viewModel.arrHomeNewsData?.count == 0 {
                    self.tableFootView.isHidden = true
                    self.noDataView.isHidden = false
                } else {
                    self.tableFootView.isHidden = false
                    self.noDataView.isHidden = true
                }
                self.dataSouce = self.viewModel.arrHomeNewsData ?? []
                if self.viewModel.hasMoreForHomeNews == false {
                    self.tableFootView.myCollectView.es.noticeNoMoreData()
                    self.tableFootView.myCollectView.footer?.alpha = 1
                }
            })
        }
    }
    
    func setupUI() {
        gk_navBarAlpha = 1
        view.backgroundColor = .white

        tableFootView = {
            let view = YHServiceTableFootView()
            view.moreBlock = {[weak self] in
                guard let self = self else { return }
                if id == 1 || id == 2 {
                    self.viewModel.getList(false ,id) {[weak self] success, error in
                        guard let self = self else { return }
                        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 0.35, execute: {
                            CATransaction.setDisableActions(true)
                            self.dataSouce = self.viewModel.arrHomeNewsData ?? []
                            CATransaction.commit()
                            self.tableFootView.myCollectView.es.stopLoadingMore()
                            if self.viewModel.hasMoreForHomeNews == false {
                                self.tableFootView.myCollectView.es.noticeNoMoreData()
                                self.tableFootView.myCollectView.footer?.alpha = 1
                            }
                        })
                    }
                } else {
                    self.viewModel.getList(false, callBackBlock: {[weak self] success, error in
                        guard let self = self else { return }
                        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 0.35, execute: {
                            CATransaction.setDisableActions(true)
                            self.dataSouce = self.viewModel.arrHomeNewsData ?? []
                            CATransaction.commit()
                            self.tableFootView.myCollectView.es.stopLoadingMore()
                            if self.viewModel.hasMoreForHomeNews == false {
                                self.tableFootView.myCollectView.es.noticeNoMoreData()
                                self.tableFootView.myCollectView.footer?.alpha = 1
                            }
                        })
                    })
                }
            }
            return view
        }()
        
        view.addSubview(tableFootView)
        tableFootView.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(k_Height_NavigationtBarAndStatuBar)
        }
        
        view.addSubview(noDataView)
        noDataView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalTo(KScreenWidth)
            make.height.equalTo(150)
        }
    }
}
