import UIKit

import UIKit

class InnerTableCell: UITableViewCell {

  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    contentView.backgroundColor = .blue
    self.textLabel?.text = "hi(\(arc4random()%70)"
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

//
// MARK :- CELL
//
class CustomTableCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {

  private let cellId = "cellId"

  lazy var tableView: UITableView = {

    let tv = UITableView(frame: .zero, style: .grouped)
    tv.translatesAutoresizingMaskIntoConstraints = false
    tv.backgroundColor = .green
    tv.delegate = self
    tv.dataSource = self
    tv.alwaysBounceVertical = false
    tv.isScrollEnabled = false

    tv.separatorColor = .red;
    tv.separatorStyle = .none;

    tv.register(InnerTableCell.self, forCellReuseIdentifier: self.cellId)
    return tv
  }()

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
  }

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! InnerTableCell
    return cell
  }

  func setupAutoLayout() {

    tableView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
    tableView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
  }

  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    contentView.backgroundColor = .white
    contentView.addSubview(tableView)
    setupAutoLayout()
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}



//
// MARK :- ViewController
//
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

  private let cellId = "cellId"

  lazy var tableView: UITableView = {

    let tv = UITableView(frame: .zero, style: .grouped)
    tv.translatesAutoresizingMaskIntoConstraints = false
    tv.backgroundColor = .lightGray
    tv.delegate = self
    tv.dataSource = self
    tv.separatorStyle = .none


    tv.register(CustomTableCell.self, forCellReuseIdentifier: self.cellId)
    return tv
  }()


  //
  // MARK :- CELL
  //
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 2
  }

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 500
    //    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomTableCell
    //    return cell.tableView.contentSize.height
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! CustomTableCell
    return cell
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    title = "TableView Demo"
    view.backgroundColor = .white
    view.addSubview(tableView)
    setupAutoLayout()
    //    UIScrollViewContentInsetAdjustmentBehavior
    tableView.contentInsetAdjustmentBehavior = .never
    automaticallyAdjustsScrollViewInsets = false
  }

  func setupAutoLayout() {

    tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  }
}

