import UIKit
import Eureka

class MultilineLabelCell: Cell<String>, CellType {
    private(set) var title: UILabel!
    private(set) var value: UILabel!
    
    required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func setup() {
        super.setup()
        
        self.selectionStyle = .none
        
        self.title = UILabel()
        self.contentView.addSubview(self.title)
        self.title.translatesAutoresizingMaskIntoConstraints = false
        self.title.font = UIFont.preferredFont(forTextStyle: .caption1)
        self.title.leadingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.leadingAnchor).isActive = true
        self.title.trailingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.trailingAnchor).isActive = true
        self.title.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 8).isActive = true
        
        self.value = UILabel()
        self.contentView.addSubview(self.value)
        self.value.translatesAutoresizingMaskIntoConstraints = false
        self.value.textColor = .secondaryLabel
        self.value.numberOfLines = 0
        self.value.textAlignment = .right
        self.value.leadingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.leadingAnchor).isActive = true
        self.value.trailingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.trailingAnchor).isActive = true
        self.value.topAnchor.constraint(equalTo: self.title.bottomAnchor, constant: 8).isActive = true
        self.value.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -8).isActive = true
    }
    
    override func update() {
        super.update()
        
        self.textLabel?.text = nil
        self.detailTextLabel?.text = nil
        
        self.title.text = row.title
        self.value.text = row.value
    }
}

final class MultilineLabelRow: Row<MultilineLabelCell>, RowType {
    required init(tag: String?) {
        super.init(tag: tag)
        cellProvider = CellProvider<MultilineLabelCell>()
    }
}
