<?php

namespace bdk\Test\Teams\Cards;

use bdk\Teams\Cards\MessageCard;
use bdk\Teams\Section;
use bdk\Test\Teams\AbstractTestCaseWith;

/**
 * @covers \bdk\Teams\AbstractItem
 * @covers \bdk\Teams\Cards\MessageCard
 */
class MessageCardTest extends AbstractTestCaseWith
{
    public function testConstruct()
    {
        $card = new MessageCard('title', 'text');
        self::assertSame(array(
            '@context' => 'http://schema.org/extensions',
            '@type' => 'MessageCard',
            'text' => 'text',
            'title' => 'title',
        ), $card->getMessage());
    }

    /**
     * @param mixed       $value
     * @param string      $type
     * @param string|null $paramName
     * @param string|null $exceptionMessage
     *
     * @dataProvider providerAssertType
     */
    public function testAssertType($value, $type, $paramName = null, $exceptionMessage = null)
    {
        if ($exceptionMessage !== null) {
            $this->expectException('InvalidArgumentException');
            $this->expectExceptionMessage($exceptionMessage);
        }
        MessageCard::assertType($value, $type, $paramName);
        self::assertTrue(true);
    }

    public function providerAssertType()
    {
        $method = __CLASS__ . '::testAssertType()';
        return [
            [array(), 'array'],
            ['call_user_func', 'callable'],
            [(object) array(), 'object'],
            [new \bdk\PubSub\Event(), 'bdk\PubSub\Event'],

            [array(), 'array|null'],
            ['call_user_func', 'callable'],
            [(object) array(), 'object|null'],
            [new \bdk\PubSub\Event(), 'bdk\PubSub\Event|null'],

            [null, 'array|null'],
            [null, 'callable|null'],
            [null, 'object|null'],
            [null, 'bdk\PubSub\Event|null'],

            [null, 'array', 'dingus', $method . ': $dingus expects array.  null provided'],
            [null, 'callable', null, $method . ' expects callable.  null provided'],
            [null, 'object', null, $method . ' expects object.  null provided'],
            [null, 'bdk\PubSub\Event', null, $method . ' expects bdk\PubSub\Event.  null provided'],

            [false, 'array|null', null, $method . ' expects array|null.  bool provided'],
            [false, 'callable|null', 'dingus', $method . ': $dingus expects callable|null.  bool provided'],
            [false, 'object|null', null, $method . ' expects object|null.  bool provided'],
            [false, 'bdk\PubSub\Event|null', null, $method . ' expects bdk\PubSub\Event|null.  bool provided'],

            [false, 'array', null, $method . ' expects array.  bool provided'],
            [false, 'callable', null, $method . ' expects callable.  bool provided'],
            [false, 'object', 'dingus', $method . ': $dingus expects object.  bool provided'],
            [false, 'bdk\PubSub\Event', null, $method . ' expects bdk\PubSub\Event.  bool provided'],
        ];
    }

    public function testConstructException()
    {
        $this->expectException('InvalidArgumentException');
        $this->expectExceptionMessage('bdk\Teams\Cards\MessageCard::__construct expects a string, numeric, stringable obj, or null. bool provided.');
        new MessageCard(true, false);
    }

    public function testGetMessage()
    {
        $card = (new MessageCard())
            ->withTitle('title')
            ->withText('text')
            ->withSummary('summary')
            ->withActivity('title', 'subtitle', 'text', 'http://example.com/cat.jpg')
            ->withColor('BEEFED')
            ->withFacts(array(
                'foo' => 'bar',
                'zip' => 'zap',
            ), 'the more you know')
            ->withHeroImage('http://example.com/dog.gif', 'dog')
            ->withImage('http://example.com/img.png', 'title')
            ->withImages(array(
                'http://example.com/img1.png',
                'http://example.com/img2.png',
            ), 'title')
            ->withAction('text', 'http://example.com/')
            ->withAddedSection(
                (new Section())
                    ->withText('how about them apples')
            );
        self::assertSame(array(
            '@context' => 'http://schema.org/extensions',
            '@type' => 'MessageCard',
            'potentialAction' => array(
                array(
                    '@context' => 'http://schema.org',
                    '@type' => 'ViewAction',
                    'name' => 'text',
                    'target' => array(
                        'http://example.com/',
                    ),
                ),
            ),
            'sections' => array(
                array(
                    'activityImage' => 'http://example.com/cat.jpg',
                    'activitySubtitle' => 'subtitle',
                    'activityText' => 'text',
                    'activityTitle' => 'title',
                ),
                array(
                    'facts' => array(
                        array(
                            'name' => 'foo',
                            'value' => 'bar',
                        ),
                        array(
                            'name' => 'zip',
                            'value' => 'zap',
                        ),
                    ),
                    'title' => 'the more you know',
                ),
                array(
                    'heroImage' => array(
                        'image' => 'http://example.com/dog.gif',
                        'title' => 'dog',
                    ),
                ),
                array(
                    'images' => array(
                        array(
                            'image' => 'http://example.com/img.png',
                        ),
                    ),
                    'title' => 'title',
                ),
                array(
                    'images' => array(
                        array(
                            'image' => 'http://example.com/img1.png',
                        ),
                        array(
                            'image' => 'http://example.com/img2.png',
                        ),
                    ),
                    'title' => 'title',
                ),
                array(
                    'text' => 'how about them apples',
                ),
            ),
            'summary' => 'summary',
            'text' => 'text',
            'themeColor' => 'BEEFED',
            'title' => 'title',
        ), $card->getMessage());
    }

    protected static function itemFactory()
    {
        return new MessageCard();
    }

    protected static function withTestCases()
    {
        return array(
            array('action', ['text', 'http://example.com/']),
            array('activity', ['title', 'subtitle', 'text', 'http://example.com/cat.jpg']),
            array('addedSection', [new Section()]),
            array('color', ['BEEFED']),
            array('facts', [array(
                'foo' => 'bar',
                'zip' => 'zap',
            ), 'the more you know']),
            array('heroImage', ['http://example.com/img.png', 'title']),
            array('image', ['http://example.com/img.png', 'title']),
            array('images', [[
                'http://example.com/img1.png',
                'http://example.com/img2.png',
            ], 'title']),
            array('summary', ['beginning, middle, end']),
            array('text', ['text']),
            array('title', ['title']),
        );
    }
}
