//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#include "interpolate.hpp"

#include "ngraph/op/constant.hpp"

using namespace std;
using namespace ngraph;

const string op::Interpolate::type_name{"Interpolate"};

op::Interpolate::Interpolate(const Output<Node>& image,
                             const Output<Node>& output_shape,
                             const InterpolateAttrs& attrs)
    : Op({image, output_shape})
    , m_attrs(attrs)
{
    constructor_validate_and_infer_types();
}

void op::Interpolate::validate_and_infer_types()
{
    NODE_VALIDATION_CHECK(this,
                          get_input_element_type(1).compatible(element::Type_t::i64),
                          "output shape must have element type i64.");
    set_input_is_relevant_to_shape(1);

    PartialShape output_shape = PartialShape(get_input_partial_shape(0));
    if (output_shape.rank().is_static())
    {
        for (auto axis : m_attrs.axes)
        {
            NGRAPH_CHECK(axis < static_cast<size_t>(output_shape.rank()));
            output_shape[axis] = Dimension::dynamic();
        }
    }

    if (auto const_shape = dynamic_pointer_cast<op::Constant>(input_value(1).get_node_shared_ptr()))
    {
        auto out_shape = static_cast<const int64_t*>(const_shape->get_data_ptr());
        size_t i = 0;
        for (auto axis : m_attrs.axes)
        {
            output_shape[axis] = Dimension(out_shape[i++]);
        }
        set_output_type(0, get_input_element_type(0), output_shape);
    }
    else
    {
        set_output_type(0, get_input_element_type(0), output_shape);
    }
}

shared_ptr<Node> op::Interpolate::copy_with_new_args(const NodeVector& new_args) const
{
    check_new_args_count(this, new_args);
    return make_shared<Interpolate>(new_args.at(0), new_args.at(1), m_attrs);
}
