describe "PM::TableViewCellModule" do

  def custom_cell
    {
      title: "Crazy Full Featured Cell",
      subtitle: "This is way too huge...",
      arguments: { data: [ "lots", "of", "data" ] },
      action: :tapped_cell_1,
      height: 50, # manually changes the cell's height
      cell_style: UITableViewCellStyleSubtitle,
      cell_identifier: "Custom Cell",
      cell_class: PM::TableViewCell,
      accessory: {
        view: :switch, # currently only :switch is supported
        value: true
      },
      image: {
        image: UIImage.imageNamed("list"),
        radius: 15
      },
      selection_style: :gray,
      properties: {
        layer: {
          masks_to_bounds: true
        },
        background_color: UIColor.redColor
      },
      accessory_type: :disclosure_indicator
    }
  end

  def attributed_cell
    {
      title: NSMutableAttributedString.alloc.initWithString("Attributed Title"),
      subtitle: NSMutableAttributedString.alloc.initWithString("Attributed Subtitle"),
      cell_style: UITableViewCellStyleSubtitle
    }
  end

  before do
    @screen = TestTableScreen.new
    button = UIButton.buttonWithType(UIButtonTypeRoundedRect).tap{|b| b.titleLabel.text = "ACC" }
    @screen.mock! :table_data do
      [
        {
          title: "", cells: []
        },
        {
          title: "",
          cells: [
            { title: "Test 1", properties: { accessory_type: UITableViewCellStateShowingEditControlMask } },
            custom_cell,
            { title: "Test2", accessory: { view: button } },
            attributed_cell,
            { title: "Image Size Test", image: {image: UIImage.imageNamed("list"), size: 20} },
          ]
        }
      ]
    end

    @screen.on_load

    @custom_ip = NSIndexPath.indexPathForRow(1, inSection: 1) # Cell "Crazy Full Featured Cell"
    @attributed_ip = NSIndexPath.indexPathForRow(3, inSection: 1) # Attributed Cell
    @image_size_ip = NSIndexPath.indexPathForRow(4, inSection: 1) # Attributed Cell

    @screen.update_table_data

    @subject = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: @custom_ip)
    @attributed_subject = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: @attributed_ip)
  end

  it "should be a PM::TableViewCell" do
    @subject.should.be.kind_of(PM::TableViewCell)
  end

  it "should have the right title" do
    @subject.textLabel.text.should == "Crazy Full Featured Cell"
  end

  it "should allow attributed title" do
    @attributed_subject.textLabel.attributedText.string.should == "Attributed Title"
  end

  it "should allow attributed subtitle" do
    @attributed_subject.detailTextLabel.attributedText.string.should == "Attributed Subtitle"
  end

  it "should have the right subtitle" do
    @subject.detailTextLabel.text.should == "This is way too huge..."
  end

  it "should have the right custom re-use identifier" do
    @subject.reuseIdentifier.should == "Custom Cell"
  end

  it "should have the right generated re-use identifier" do
    ip = NSIndexPath.indexPathForRow(2, inSection: 1)
    subject = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: ip)
    subject.reuseIdentifier.should == "ProMotion::TableViewCell-accessory"
  end

  it "should have the correct height" do
    @screen.tableView(@screen.table_view, heightForRowAtIndexPath: @custom_ip).should == 50
  end

  it "should set the layer.masksToBounds" do
    @subject.layer.masksToBounds.should == true
  end

  it "should set the background color" do
    @subject.backgroundColor.should == UIColor.redColor
  end

  it "should set the selection color style" do
    @subject.selectionStyle.should == UITableViewCellSelectionStyleGray
  end

  it "should set the accessory view to a switch" do
    @subject.accessoryView.should.be.kind_of(UISwitch)
  end

  it "should set the accessory view to a button" do
    ip = NSIndexPath.indexPathForRow(2, inSection: 1)
    subject = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: ip)
    subject.accessoryView.should.be.kind_of(UIButton)
  end

  it "should set the accessory type to edit" do
    ip = NSIndexPath.indexPathForRow(0, inSection: 1)
    subject = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: ip)
    subject.accessoryView.should.be.nil
    subject.accessoryType.should == UITableViewCellStateShowingEditControlMask
  end

  it "should set an image with a radius" do
    @subject.imageView.should.be.kind_of(UIImageView)
    @subject.imageView.image.should == UIImage.imageNamed("list")
    @subject.imageView.layer.cornerRadius.should == 15.0
  end

  it "should set an image size" do
    cell_for_height = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:"CellForHeight")
    default_cell_height = cell_for_height.frame.size.height

    @subject.layoutSubviews
    @subject.imageView.frame.size.should == UIImage.imageNamed("list").size

    image_cell = @screen.tableView(@screen.table_view, cellForRowAtIndexPath: @image_size_ip)
    image_cell.layoutSubviews

    image_cell.imageView.frame.origin.should.not == CGPointMake(0,0)
    image_cell.imageView.bounds.origin.should == CGPointMake(0,0)

    image_cell.imageView.frame.size.should == CGSizeMake(20,20)
    image_cell.imageView.bounds.size.should == CGSizeMake(20,20)
  end

  it "should have the proper accessory type" do
    @subject.accessoryType.should == UITableViewCellAccessoryDisclosureIndicator
  end

end