//
//  YHDavidCell0.swift
//  galaxy
//
//  Created by davidhuangA on 2024/1/17.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHDavidCell5: UITableViewCell {
    static let cellHeight : CGFloat = 219
    static let cellReuseIdentifier = "YHDavidCell5"
    
    
    let items : [Int32] = [1,2,3,4,5,6,7,8]

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        setupUI()
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
  
    lazy var subContentView : UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    var myCollectView : UICollectionView!
    
    
//    lazy var myCollectView = {
//        // 设置布局方向
//
//        
//        
//        return collectinoView
//    }()
    
    func setupUI() {
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        
        contentView.addSubview(subContentView)
        subContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let bbxLable = UILabel()
        bbxLable.text = "百宝箱"
        bbxLable.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        bbxLable.font = kFont(size: 18)
        
        subContentView.addSubview(bbxLable)
        bbxLable.snp.makeConstraints { make in
            make.top.equalTo(13)
            make.left.equalTo(25)
        }
        
        let desLable = UILabel()
        desLable.text = "香港本地宝，智慧小助手"
        desLable.frame = CGRect(x: 0, y: 0, width: 141.5, height: 11.5)
        desLable.textColor = UIColor(red: 0.592, green: 0.592, blue: 0.592, alpha: 1)
        desLable.font = kFont(size: 10)
        subContentView.addSubview(desLable)
        desLable.snp.makeConstraints { make in
            make.left.equalTo(bbxLable.snp.right).offset(11)
            make.centerY.equalTo(bbxLable)
        }
        
        
//        subContentView.backgroundColor = UIColor(hex: 0x999999)
        
        let flowLayout = UICollectionViewFlowLayout()
        let margin = 16.0
        let gap = 20.0
        let itemWidth = 80.0
        flowLayout.itemSize = CGSize(width: itemWidth, height: 64)
        flowLayout.minimumInteritemSpacing = (KScreenWidth - 2 * 16 - 2*16 - 4 * itemWidth)/4
        flowLayout.minimumLineSpacing = 15.0
        flowLayout.scrollDirection = .vertical
        
        let collectinoView = YHGestureCollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collectinoView.contentInset = UIEdgeInsets(top: 0, left: margin, bottom: 20, right: margin)
        collectinoView.backgroundColor = .white
        collectinoView.register(YHBbxCollectionViewCell.self, forCellWithReuseIdentifier:YHBbxCollectionViewCell.cellReuseIdentifier)
        collectinoView.delegate = self
        collectinoView.dataSource = self
        
        collectinoView.translatesAutoresizingMaskIntoConstraints = false
        collectinoView.alwaysBounceVertical = true
        myCollectView = collectinoView
        myCollectView.isScrollEnabled = false
        
        subContentView.addSubview(myCollectView)
        myCollectView.snp.makeConstraints { make in
            make.top.equalTo(bbxLable.snp.bottom).offset(15)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-20)
        }
        
    }
}

extension YHDavidCell5: 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: YHBbxCollectionViewCell.cellReuseIdentifier, for: indexPath) as! YHBbxCollectionViewCell
        return cell
        
    }
    
    private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("index is \(indexPath.row)")
    }
}
