//
//  YHDavidTestViewController.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/10.
//  Copyright © 2024 www.davidhuang.com. All rights reserved.
//

import UIKit
import ESPullToRefresh
import JXSegmentedView
import GKNavigationBarSwift

class YHDavidTestViewController: YHBaseViewController {
    var vcCanScroll : Bool = true
    weak var myParentVc : ContentBaseViewController?
    
    lazy var descripeLable:UILabel = {
        let label = UILabel()
        label.text = "赴港攻略，当日往返无忧，这个季节风景..赴港攻略，当日往返无忧，这个季节风景"
        label.textColor = UIColor(hexString:"#333333")
        label.font = UIFont.systemFont(ofSize: 10)
        label.numberOfLines = 2
        label.lineBreakMode = .byTruncatingTail
        return label
    }()
    
    
    lazy var headImageView:UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "home_person_icon"))
        imageV.contentMode = .scaleAspectFill
        return imageV
    }()
    
    
    lazy var nameLable:UILabel = {
        let label = UILabel()
        label.text = "Fiona"
        label.textColor = UIColor(hexString:"#333333")
        label.font = UIFont.systemFont(ofSize: 10)
        return label
    }()
    
    
    lazy var dateLable:UILabel = {
        let label = UILabel()
        label.text = "12-12"
        label.textColor = UIColor(hexString:"#333333")
        label.font = UIFont.systemFont(ofSize: 10)
        return label
    }()
    
    
    lazy var heartImageView:UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "home_red_heart"))
        imageV.contentMode = .scaleAspectFill
        return imageV
    }()
    
    lazy var numLable:UILabel = {
        let label = UILabel()
        label.text = "2000"
        label.textColor = UIColor(hexString:"#333333")
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    }()
    
    
    lazy var items = {
        return [AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高")]
    }()
    
    
    private lazy var bannerView: YHHomeBannerView = {
        let view = YHHomeBannerView()
        return view
    }()
    
    lazy var myCollectView = {
        // 设置布局方向
        let flowLayout = UICollectionViewFlowLayout()
        let margin = 20.0
        let gap = 20.0
        let itemWidth = ceil((KScreenWidth - 2*margin - gap)/2.0)
        flowLayout.itemSize = CGSize(width: itemWidth, height: 210 + 87 + 4)
        flowLayout.minimumInteritemSpacing = 10.0
        flowLayout.minimumLineSpacing = 10.0
        flowLayout.scrollDirection = .vertical
        
        let collectinoView = YHGestureCollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collectinoView.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        collectinoView.backgroundColor = .white
        collectinoView.register(YHAboutUsAdvantageCell.self, forCellWithReuseIdentifier:YHAboutUsAdvantageCell.cellReuseIdentifier)
        collectinoView.register(YHDavidTestCollectionViewCell.self, forCellWithReuseIdentifier:YHDavidTestCollectionViewCell.cellReuseIdentifier)
        collectinoView.delegate = self
        collectinoView.dataSource = self
        
        collectinoView.translatesAutoresizingMaskIntoConstraints = false
        collectinoView.alwaysBounceVertical = true
        return collectinoView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
        view.backgroundColor = .white
        gk_navigationBar.isHidden = true
        self.items = [AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高")]
        self.myCollectView.reloadData()
    }
}

extension YHDavidTestViewController {
    
    private func loadFirstItem() {
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.items = [AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高")]
            self.myCollectView.es.stopPullToRefresh(ignoreDate: true,ignoreFooter: false)
            self.myCollectView.reloadData()
        }
        
    }
    
    private func loadFakeData() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"))
            self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"))
            self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"))
            self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"))
            self.items.append(AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"))
            
            self.myCollectView.es.stopLoadingMore()
            self.myCollectView.reloadData()
        }
    }
    
    func setupUI() {
        gk_navTitle = "David的测试UI"
        view.backgroundColor = .white
        view.addSubview(myCollectView)
        myCollectView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(10)
        }
        self.myCollectView.es.addInfiniteScrolling {
            self.loadFakeData()
        }
    }
}



extension YHDavidTestViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: YHDavidTestCollectionViewCell.cellReuseIdentifier, for: indexPath) as! YHDavidTestCollectionViewCell
        return cell
        
    }
    
    private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("index is \(indexPath.row)")
    }
}


// MARK: - JXSegmentedListContainerViewListDelegate
extension YHDavidTestViewController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}


// MARK: - 滑动使用
extension YHDavidTestViewController: UIScrollViewDelegate {
    //控制滑动交互
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let scrollViewSet: CGFloat = scrollView.contentOffset.y
        if vcCanScroll == false {//不能滚动时
            scrollView.contentOffset = CGPoint(x: -20, y: 0)
        } else {
            if (scrollViewSet <= 0) {
                scrollView.contentOffset = CGPoint(x: -20, y: 0)
                vcCanScroll = false
                NotificationCenter.default.post(name:  Notification.Name(rawValue: "leaveTop"), object: nil)
            } else {
                scrollView.contentOffset = CGPoint(x: -20, y: scrollViewSet)
            }
        }
    }
}
