//
//  SelectionItemView.swift
//  MobilePortal
//
//  Created by caiyang on 2020/4/10.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

class SelectionItemView: UIView {
    
    var yH = CGFloat()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    func addItemView(selectionArray:NSMutableArray) {
//        self.removeFromSuperview()
        if self.subviews.count >= selectionArray.count {
            return
        }
        yH = 0
        for i in 0..<selectionArray.count {
            let selectionModel:SelectionModel = selectionArray[i] as! SelectionModel
            let titleLabel = self.getTitleLabel(itemModel: selectionModel)
            titleLabel.frame = CGRect(x:0, y: yH, width:self.width , height:selectionModel.titleH - selectionModel.intervalH )
            yH = yH + selectionModel.titleH
            for index in 0..<selectionModel.selectItemArray.count {
                let itemModel:DialogueListItemModel = selectionModel.selectItemArray[index] as! DialogueListItemModel
                let sulabel = self.getSubLabel(itemModel: itemModel)
                sulabel.frame = CGRect(x:0, y: yH, width:self.width , height:itemModel.itemH - selectionModel.intervalH)
                yH = yH + itemModel.itemH
            }
        }
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func getTitleLabel(itemModel:SelectionModel)->UILabel{
        let label = UILabel.init()
        label.text = itemModel.typename
        label.textColor = UIColorFromRGB(rgbValue: 0x3D444F)
        label.font = UIFont.test_systemFont(ofSize: 13.33)
        label.attributeColor = UIColorFromRGB(rgbValue: 0x3D444F)
        label.underLine = true
        label.throughLine = false
        self.addSubview(label)
        label.addAttributeTapAction(with: [itemModel.typename!]) { (string, range, index) in
        }
        
        return label
    }
    func getSubLabel(itemModel:DialogueListItemModel)->UILabel{
        let label = UILabel.init()
        label.text = itemModel.title
        label.textColor = UIColorFromRGB(rgbValue: 0x366EEC)
        label.font = UIFont.test_systemFont(ofSize: 13.33)
        label.numberOfLines = 0
        label.attributeColor = UIColorFromRGB(rgbValue: 0x366EEC)
        label.underLine = true
        label.throughLine = false
        label.addAttributeTapAction(with: [itemModel.title!]) { (string, range, index) in
            itemModel.block()
        }
        self.addSubview(label)
        return label
    }
}
