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

import UIKit

class YHHomeCustomCell: UITableViewCell {
    static let cellReuseIdentifier = "YHHomeCustomCell"
    
    lazy var items = {
       
        return [AboutAdvantageItem(iconName: "about_match", title: "精准匹配", detail: "大数据精准匹配，专属方案获批率更高"),
                AboutAdvantageItem(iconName: "about_response", title: "快速响应", detail: "身份规划师一对一专业服务，第一时间响应您的需求"),
                AboutAdvantageItem(iconName: "about_service", title: "专业服务", detail: "5年经验+英语专8级文案导师团队倾力支持"),
                AboutAdvantageItem(iconName: "about_service2", title: "精准匹配", detail: "香港专才、香港留学获批常年保持90%以上")]
    }()
    
    
    var dataArray: [String] = [] // 假定我们的数据结构就是一个String数组
    var collectionView: UICollectionView!
    var collectionContainer: UIView!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 设置布局方向
        let flowLayout = UICollectionViewFlowLayout()
        let margin = 18.0
        let gap = 12.0
        let itemWidth = floor((KScreenWidth-2*margin-gap)/2.0)
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumInteritemSpacing = 12.0
        flowLayout.minimumLineSpacing = 12.0
        flowLayout.scrollDirection = .vertical
        
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collectionView.contentInset = UIEdgeInsets(top: 0, left: margin, bottom: 0, right: margin)
        collectionView.backgroundColor = UIColor.white
        collectionView.register(YHAboutUsAdvantageCell.self, forCellWithReuseIdentifier:YHAboutUsAdvantageCell.cellReuseIdentifier)
        collectionView.delegate = self
        
        
        collectionContainer = UIView(frame: .zero)
        contentView.addSubview(collectionContainer)
        
        collectionContainer.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(0)
        }
        
        collectionContainer.addSubview(collectionView)
        
        collectionView.snp.makeConstraints({ make in
            make.edges.equalToSuperview()
        })
//        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "test")
//        collectionView.delegate = self // 这里根据具体业务写吧，demo中我就把代理直接设置成cell了..
//        collectionView.dataSource = self // 根据业务，demo直接先cell持有数据来控制collectionView的显示了..
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 外部调用，用来设置collectionView的数据源
    func setData(_ data: [String]) {
        self.dataArray = data
        collectionView.reloadData()
        // 这里需要 layoutIfNeeded 一下，否则我们不能同步拿到contentSize
        // 注意，如果上面collectionContainer的布局没有指明宽度，这个时候cell的宽度是estimate状态，所以下面就算layout也拿不到正确的contentSize
        collectionView.layoutIfNeeded()
        
        let height = collectionView.contentSize.height
        printLog(height)
        
        collectionContainer.snp.updateConstraints { make in
            make.height.equalTo(height)
        }
    }
    
}


extension YHHomeCustomCell: 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: YHAboutUsAdvantageCell.cellReuseIdentifier, for: indexPath) as! YHAboutUsAdvantageCell
        cell.advantateItem = items[indexPath.row]
        return cell

    }
        
    private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("index is \(indexPath.row)")
    }
}
