//
//  CWBaseCollectionViewCell.swift
//  ClockWidget
//
//  Created by fff on 2024/11/12.
//


class TSBaseCollectionCell: UICollectionViewCell,ComponentView  {
    
    var colComponent:CollectionViewComponent?
    var colAttributes:[String : Any]?
    
    lazy var bgContentView:UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.addSubview(bgContentView)
        bgContentView.snp.makeConstraints { make in
            make.top.leading.trailing.bottom.equalTo(0)
        }
        creatUI()
        dealThings()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func creatUI(){
        
    }
    
    func dealThings(){
        
    }
    
    func renderView(with object: Any?, component: CollectionViewComponent, attributes: [String : Any]?) {
        self.colComponent = component
        self.colAttributes = attributes
    }
    
    deinit {
        debugPrint("<---deinit \(String(describing: self))")
    }
    
}

extension TSBaseCollectionCell{
    var indexPath:IndexPath?{
        if let attributes = colAttributes , let IndexPath = attributes[kIndexPath] as? IndexPath{
            return IndexPath
        }
        return nil
    }
    
    var itemActionHandler: ((Any?, IndexPath) -> Void)?{
        if let colComponent = colComponent,let itemActionHandler = colComponent.itemActionHandler {
            return itemActionHandler
        }
        return nil
    }
    
    func actionHandler(any:Any?){
        if let sectionActionHandler = itemActionHandler ,let indexPath = indexPath{
            sectionActionHandler(any,indexPath)
        }
    }
}






class TSBaseCollectionnReusableView : UICollectionReusableView ,ComponentView  {
    
    var colComponent:CollectionViewComponent?
    var colAttributes:[String : Any]?
    
    lazy var bgContentView:UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.addSubview(bgContentView)
        bgContentView.snp.makeConstraints { make in
            make.top.leading.trailing.bottom.equalTo(0)
        }
        creatUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func creatUI(){
        
    }

    func renderView(with object: Any?, component: CollectionViewComponent, attributes: [String : Any]?) {
        self.colComponent = component
        self.colAttributes = attributes
    }
    
    
    deinit {
        debugPrint("<---deinit \(String(describing: self))")
    }
    
}

extension TSBaseCollectionnReusableView{
    var indexPath:IndexPath?{
        if let attributes = colAttributes , let IndexPath = attributes[kIndexPath] as? IndexPath{
            return IndexPath
        }
        return nil
    }
    
    var sectionActionHandler: ((Any?, IndexPath) -> Void)?{
        if let colComponent = colComponent,let sectionActionHandler = colComponent.sectionActionHandler {
            return sectionActionHandler
        }
        return nil
    }
    
    func actionHandler(any:Any?){
        if let sectionActionHandler = sectionActionHandler ,let indexPath = indexPath{
            sectionActionHandler(any,indexPath)
        }
    }
}
