<?php

define('MKBH_D6_MIGRATION_FRONTEND_ENDPOINT', 'http://www.magasinetkbh.dk');
define('MKBH_D6_MIGRATION_API_ENDPOINT', 'http://www.magasinetkbh.dk/api/migration');
define('MKBH_D6_MIGRATION_API_CLIENT_ID', '35092712124368907385');
define('MKBH_D6_MIGRATION_API_CLIENT_SECRET', 'fee45c9369481d5e6b324ce851f7a21809e593a6');

/**
 * Implements hook_init().
 */
function mkbh_migration_init()
{
    module_load_include('inc', 'mkbh_migration', 'mkbh_migration.helpers');
}

/**
 * Implements hook_menu().
 */
function mkbh_migration_menu()
{
    $items['admin/config/system/content-migration'] = [
        'title' => 'Magasinet KBH Content Migration',
        'description' => 'Perform D6->D7 content migration',
        'access arguments' => ['administer site configuration'],
        'page callback' => 'drupal_get_form',
        'page arguments' => ['mkbh_migration_perform_migration_form'],
        'file' => 'mkbh_migration.forms.inc',
        'type' => MENU_NORMAL_ITEM,
    ];

    return $items;
}

/**
 * Implements hook_node_delete().
 *
 * @param $node
 */
function mkbh_migration_node_delete($node)
{
    db_delete('mkbh_migration_imported_map')->condition('imported_node_id', $node->nid, '=')->execute();

    db_delete('url_alias')->condition('source', 'node/' . $node->nid, '=')->execute();
    path_delete(['source' => 'node/' . $node->nid]);
}

/**
 * BatchAPI Worker callback.
 *
 * @param $remoteNode
 * @param $context
 */
function mkbh_migration_perform_migration_batch_worker($remoteNode, &$context)
{
    try {
        $nodeAdapter = new \Drupal\mkbh_migration\Adapter\RemoteNodeAdapter($remoteNode);

        $entity = $nodeAdapter->toLocalEntity();

        if (empty($context['results']['skipped_operations'])) {
            $context['results']['skipped_operations'] = 0;
        }

        if (empty($context['results']['failed_operations'])) {
            $context['results']['failed_operations'] = 0;
        }

        if (empty($context['results']['successful_operations'])) {
            $context['results']['successful_operations'] = 0;
        }

        if ($entity === FALSE) {
            $context['results']['skipped_operations']++;
        } elseif ($entity instanceof \Exception) {
            $context['results']['failed_operations']++;
        } else {
            $context['results']['successful_operations']++;
        }
    } catch (Exception $e) {
        $context['results']['failed_operations']++;
        watchdog('mkbh_migration', 'Failed to import node. RemoteNodeAdapter said: ' . $e->getMessage(), [], WATCHDOG_ERROR);
    }
}

/**
 * BatchAPI Finished callback.
 *
 * @param $success
 * @param $results
 * @param $operations
 */
function mkbh_migration_perform_migration_batch_finished($success, $results, $operations)
{
    if (!empty($results['failed_operations'])) {
        drupal_set_message(format_string('Something went wrong and BatchAPI indicated there was an error in your batch. Number of operations failed processing: @count', [
            '@count' => (int)$results['failed_operations'],
        ]), 'error');
    } else {
        drupal_set_message(format_string('Content import has finished, @count nodes were imported, @skipped were skipped. You may now proceed to your website to review results.', [
            '@count' => (int)$results['successful_operations'],
            '@skipped' => (int)$results['skipped_operations'],
        ]), 'status');
    }
}
