//
//  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
    
    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
        
        
    }
}

extension YHDavidTestViewController {
    
    private func loadFirstItem() {
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.items = [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.myCollectView.es.stopLoadingMore()
            self.myCollectView.reloadData()
        }
    }
    
    func setupUI() {
        gk_navTitle = "David的测试UI"
        view.backgroundColor = .white
//        view.addSubview(bannerView)
//        
//        let bannerHeight: CGFloat = 143/343 * (KScreenWidth - 16 * 2)
//        bannerView.snp.makeConstraints { make in
//            make.top.left.right.equalToSuperview()
//            make.height.equalTo(bannerHeight)
//        }
//        
//        let subHoldView = UIView()
//        subHoldView.layer.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor
//        subHoldView.layer.borderWidth = 0.5
//        subHoldView.layer.borderColor = UIColor(red: 0.798, green: 0.798, blue: 0.798, alpha: 1).cgColor
//        subHoldView.backgroundColor = .red
//        view.addSubview(subHoldView)
//        subHoldView.snp.makeConstraints { make in
//            make.top.equalTo(bannerView.snp.bottom).offset(2)
//            make.left.equalToSuperview().offset(16)
//            make.right.equalToSuperview().offset(-16)
//            make.height.equalTo(87)
//        }
//        
//        subHoldView.addSubview(descripeLable)
//        descripeLable.snp.makeConstraints { make in
//            make.top.equalToSuperview().offset(8)
//            make.left.equalToSuperview().offset(8)
//            make.right.equalToSuperview().offset(-8)
//            make.height.equalTo(30)
//        }
//        
//        subHoldView.addSubview(headImageView)
//        headImageView.snp.makeConstraints { make in
//            make.top.equalTo(descripeLable.snp.bottom).offset(10)
//            make.left.equalTo(descripeLable.snp.left)
//            make.height.width.equalTo(25)
//        }
//        
//        subHoldView.addSubview(nameLable)
//        nameLable.snp.makeConstraints { make in
//            make.top.equalTo(headImageView.snp.top)
//            make.left.equalTo(headImageView.snp.right).offset(5)
//            make.height.equalTo(12)
//            make.width.equalTo(50)
//        }
//        
//        subHoldView.addSubview(dateLable)
//        dateLable.snp.makeConstraints { make in
//            make.top.equalTo(nameLable.snp.bottom).offset(2)
//            make.left.equalTo(nameLable.snp.left)
//            make.height.equalTo(12)
//            make.right.equalTo(nameLable.snp.right)
//        }
//        
//        subHoldView.addSubview(numLable)
//        numLable.snp.makeConstraints { make in
//            make.right.equalToSuperview().offset(-8)
//            make.height.equalTo(12)
//            make.centerY.equalTo(headImageView.snp.centerY)
//        }
//        
//        subHoldView.addSubview(heartImageView)
//        heartImageView.snp.makeConstraints { make in
//            make.right.equalTo(numLable.snp.left).offset(-6)
//            make.height.width.equalTo(12)
//            make.centerY.equalTo(headImageView.snp.centerY)
//        }
        
        view.addSubview(myCollectView)
        myCollectView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(10)
        }
        
        
//        self.myCollectView.backgroundColor = UIColor.green
        self.myCollectView.es.addPullToRefresh {
            self.loadFirstItem()
        }
//        self.myCollectView.es.startPullToRefresh()
        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) {
        if scrollView === myCollectView {
            let scrollViewSet: CGFloat = scrollView.contentOffset.y
            printLog("subTableView --- scrollView.contentOffset.y = \(scrollViewSet)   scrollView.contentOffset.x = \(scrollView.contentOffset.y)")
            if vcCanScroll == false {//不能滚动时
                scrollView.contentOffset = CGPoint(x: -20, y: 0)
                
                printLog("vcCanScroll == false")
            } 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)
                }
            }
        }
    }
}
