//
//  TSBasicItemModel.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

class TSBasicItemModel {
    // MARK: - Properties
    var index: Int = 0
//    var clickBlock: ((Any?) -> Void)?
    var tapBlock: ((TSBasicItemModel, Int, Any?) -> Void)?
    var updateDataBlock: ((TSBasicItemModel) -> Void)?
    
    var leftImageName: String?
    var leftImage: UIImage?
    var leftImagePath: String?
    var leftTitle: String?
    var leftSubTitle: String?
    var rightString: String?
    var rightSubString: String?
    var rightIntValue: Int = 0
    var itemData: Any?
    var identifier: String?
    var parametView: UIView?
    var rightIsHave: Bool = false
    var bottomIsHave: Bool = false
    var isSelected: Bool = false
    var isWhether: Bool = false
    var rightViewStyle: Int = 0
    var rectCorner: UIRectCorner = []
    var topSpace: Bool = false
    var bottomSpace: Bool = false
    var height: CGFloat = 0.0
    var width: CGFloat = 0.0
    var type: Int = 0
    weak var attributionSection: TSBasicSectionModel?
    
    // MARK: - Methods
    func setCornerView(_ view: UIView, corner: CGFloat) {
        if rectCorner.isEmpty {
            view.layer.cornerRadius = 0
        } else {
            let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: rectCorner, cornerRadii: CGSize(width: corner, height: corner))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            view.layer.mask = mask
        }
    }
    
    func updateData() {
        updateDataBlock?(self)
    }
    
    
    static func createItemModel(leftTitle: String,
                                rightViewStyle: Int,
                                rightString: String,
                                rightIsHave: Bool,
                                height: CGFloat,
                                rectCorner: UIRectCorner,
                                tapBlock: @escaping ((TSBasicItemModel, Int, Any?) -> Void)) -> TSBasicItemModel {
        let model = TSBasicItemModel()
        model.leftTitle = leftTitle
        model.rightViewStyle = rightViewStyle
        model.rightString = rightString
        model.rightIsHave = rightIsHave
        model.height = height
        model.rectCorner = rectCorner
        model.tapBlock = tapBlock
        return model
    }
    
    
    static func setEdge(with dataArray: [TSBasicItemModel]) {
        guard !dataArray.isEmpty else { return }
        if dataArray.count == 1 {
            dataArray.first?.rectCorner = .allCorners
        } else {
            dataArray.first?.rectCorner = [.topLeft, .topRight]
            dataArray.last?.rectCorner = [.bottomLeft, .bottomRight]
        }
    }
}



class TSBasicSectionModel {
    // MARK: - Properties
    var index: Int = 0
    var sectionData: Any?
    var leftTitle: String?
    var leftSubTitle: String?
    var rightString: String?
    var rightSubString: String?
    var rightIsHave: Bool = false
    var bottomIsHave: Bool = false
    var topIsHave: Bool = false
    var isOpen: Bool = false
    var type: Int = 0
    var rectCorner: UIRectCorner = []
    var height: CGFloat = 0.0
    var isFooter: Bool = false
    var itemsArray: [TSBasicItemModel] = []
    var selectedItemsIndex: Int = 0
    
    // MARK: - Methods
    func setCornerView(_ view: UIView, corner: CGFloat) {
        if rectCorner.isEmpty {
            view.layer.cornerRadius = 0
        } else {
            let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: rectCorner, cornerRadii: CGSize(width: corner, height: corner))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            view.layer.mask = mask
        }
    }
    
    func addSubItemModel(_ itemModel: TSBasicItemModel) {
        itemModel.attributionSection = self
        itemsArray.append(itemModel)
    }
    
    static func createSectionModel(leftTitle: String, height: CGFloat, index: Int = 0) -> TSBasicSectionModel {
        let model = TSBasicSectionModel()
        model.leftTitle = leftTitle
        model.height = height
        model.index = index
        return model
    }
    
    static func setEdge(with dataArray: [TSBasicSectionModel]) {
        for section in dataArray {
            let itemsArray = section.itemsArray
            guard !itemsArray.isEmpty else { continue }
            if itemsArray.count == 1 {
                itemsArray.first?.rectCorner = .allCorners
            } else {
                itemsArray.first?.rectCorner = [.topLeft, .topRight]
                itemsArray.last?.rectCorner = [.bottomLeft, .bottomRight]
            }
        }
    }
}
