/*
 * Copyright (c) 2017-2019 THL A29 Limited, a Tencent company. All Rights Reserved.
 *
 * 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 <tencentcloud/ocr/v20181119/model/TextDetectionEn.h>

using TencentCloud::CoreInternalOutcome;
using namespace TencentCloud::Ocr::V20181119::Model;
using namespace rapidjson;
using namespace std;

TextDetectionEn::TextDetectionEn() :
    m_detectedTextHasBeenSet(false),
    m_confidenceHasBeenSet(false),
    m_polygonHasBeenSet(false),
    m_advancedInfoHasBeenSet(false),
    m_wordCoordPointHasBeenSet(false),
    m_candWordHasBeenSet(false),
    m_wordsHasBeenSet(false)
{
}

CoreInternalOutcome TextDetectionEn::Deserialize(const Value &value)
{
    string requestId = "";


    if (value.HasMember("DetectedText") && !value["DetectedText"].IsNull())
    {
        if (!value["DetectedText"].IsString())
        {
            return CoreInternalOutcome(Error("response `TextDetectionEn.DetectedText` IsString=false incorrectly").SetRequestId(requestId));
        }
        m_detectedText = string(value["DetectedText"].GetString());
        m_detectedTextHasBeenSet = true;
    }

    if (value.HasMember("Confidence") && !value["Confidence"].IsNull())
    {
        if (!value["Confidence"].IsInt64())
        {
            return CoreInternalOutcome(Error("response `TextDetectionEn.Confidence` IsInt64=false incorrectly").SetRequestId(requestId));
        }
        m_confidence = value["Confidence"].GetInt64();
        m_confidenceHasBeenSet = true;
    }

    if (value.HasMember("Polygon") && !value["Polygon"].IsNull())
    {
        if (!value["Polygon"].IsArray())
            return CoreInternalOutcome(Error("response `TextDetectionEn.Polygon` is not array type"));

        const Value &tmpValue = value["Polygon"];
        for (Value::ConstValueIterator itr = tmpValue.Begin(); itr != tmpValue.End(); ++itr)
        {
            Coord item;
            CoreInternalOutcome outcome = item.Deserialize(*itr);
            if (!outcome.IsSuccess())
            {
                outcome.GetError().SetRequestId(requestId);
                return outcome;
            }
            m_polygon.push_back(item);
        }
        m_polygonHasBeenSet = true;
    }

    if (value.HasMember("AdvancedInfo") && !value["AdvancedInfo"].IsNull())
    {
        if (!value["AdvancedInfo"].IsString())
        {
            return CoreInternalOutcome(Error("response `TextDetectionEn.AdvancedInfo` IsString=false incorrectly").SetRequestId(requestId));
        }
        m_advancedInfo = string(value["AdvancedInfo"].GetString());
        m_advancedInfoHasBeenSet = true;
    }

    if (value.HasMember("WordCoordPoint") && !value["WordCoordPoint"].IsNull())
    {
        if (!value["WordCoordPoint"].IsArray())
            return CoreInternalOutcome(Error("response `TextDetectionEn.WordCoordPoint` is not array type"));

        const Value &tmpValue = value["WordCoordPoint"];
        for (Value::ConstValueIterator itr = tmpValue.Begin(); itr != tmpValue.End(); ++itr)
        {
            WordCoordPoint item;
            CoreInternalOutcome outcome = item.Deserialize(*itr);
            if (!outcome.IsSuccess())
            {
                outcome.GetError().SetRequestId(requestId);
                return outcome;
            }
            m_wordCoordPoint.push_back(item);
        }
        m_wordCoordPointHasBeenSet = true;
    }

    if (value.HasMember("CandWord") && !value["CandWord"].IsNull())
    {
        if (!value["CandWord"].IsArray())
            return CoreInternalOutcome(Error("response `TextDetectionEn.CandWord` is not array type"));

        const Value &tmpValue = value["CandWord"];
        for (Value::ConstValueIterator itr = tmpValue.Begin(); itr != tmpValue.End(); ++itr)
        {
            CandWord item;
            CoreInternalOutcome outcome = item.Deserialize(*itr);
            if (!outcome.IsSuccess())
            {
                outcome.GetError().SetRequestId(requestId);
                return outcome;
            }
            m_candWord.push_back(item);
        }
        m_candWordHasBeenSet = true;
    }

    if (value.HasMember("Words") && !value["Words"].IsNull())
    {
        if (!value["Words"].IsArray())
            return CoreInternalOutcome(Error("response `TextDetectionEn.Words` is not array type"));

        const Value &tmpValue = value["Words"];
        for (Value::ConstValueIterator itr = tmpValue.Begin(); itr != tmpValue.End(); ++itr)
        {
            Words item;
            CoreInternalOutcome outcome = item.Deserialize(*itr);
            if (!outcome.IsSuccess())
            {
                outcome.GetError().SetRequestId(requestId);
                return outcome;
            }
            m_words.push_back(item);
        }
        m_wordsHasBeenSet = true;
    }


    return CoreInternalOutcome(true);
}

void TextDetectionEn::ToJsonObject(Value &value, Document::AllocatorType& allocator) const
{

    if (m_detectedTextHasBeenSet)
    {
        Value iKey(kStringType);
        string key = "DetectedText";
        iKey.SetString(key.c_str(), allocator);
        value.AddMember(iKey, Value(m_detectedText.c_str(), allocator).Move(), allocator);
    }

    if (m_confidenceHasBeenSet)
    {
        Value iKey(kStringType);
        string key = "Confidence";
        iKey.SetString(key.c_str(), allocator);
        value.AddMember(iKey, m_confidence, allocator);
    }

    if (m_polygonHasBeenSet)
    {
        Value iKey(kStringType);
        string key = "Polygon";
        iKey.SetString(key.c_str(), allocator);
        value.AddMember(iKey, Value(kArrayType).Move(), allocator);

        int i=0;
        for (auto itr = m_polygon.begin(); itr != m_polygon.end(); ++itr, ++i)
        {
            value[key.c_str()].PushBack(Value(kObjectType).Move(), allocator);
            (*itr).ToJsonObject(value[key.c_str()][i], allocator);
        }
    }

    if (m_advancedInfoHasBeenSet)
    {
        Value iKey(kStringType);
        string key = "AdvancedInfo";
        iKey.SetString(key.c_str(), allocator);
        value.AddMember(iKey, Value(m_advancedInfo.c_str(), allocator).Move(), allocator);
    }

    if (m_wordCoordPointHasBeenSet)
    {
        Value iKey(kStringType);
        string key = "WordCoordPoint";
        iKey.SetString(key.c_str(), allocator);
        value.AddMember(iKey, Value(kArrayType).Move(), allocator);

        int i=0;
        for (auto itr = m_wordCoordPoint.begin(); itr != m_wordCoordPoint.end(); ++itr, ++i)
        {
            value[key.c_str()].PushBack(Value(kObjectType).Move(), allocator);
            (*itr).ToJsonObject(value[key.c_str()][i], allocator);
        }
    }

    if (m_candWordHasBeenSet)
    {
        Value iKey(kStringType);
        string key = "CandWord";
        iKey.SetString(key.c_str(), allocator);
        value.AddMember(iKey, Value(kArrayType).Move(), allocator);

        int i=0;
        for (auto itr = m_candWord.begin(); itr != m_candWord.end(); ++itr, ++i)
        {
            value[key.c_str()].PushBack(Value(kObjectType).Move(), allocator);
            (*itr).ToJsonObject(value[key.c_str()][i], allocator);
        }
    }

    if (m_wordsHasBeenSet)
    {
        Value iKey(kStringType);
        string key = "Words";
        iKey.SetString(key.c_str(), allocator);
        value.AddMember(iKey, Value(kArrayType).Move(), allocator);

        int i=0;
        for (auto itr = m_words.begin(); itr != m_words.end(); ++itr, ++i)
        {
            value[key.c_str()].PushBack(Value(kObjectType).Move(), allocator);
            (*itr).ToJsonObject(value[key.c_str()][i], allocator);
        }
    }

}


string TextDetectionEn::GetDetectedText() const
{
    return m_detectedText;
}

void TextDetectionEn::SetDetectedText(const string& _detectedText)
{
    m_detectedText = _detectedText;
    m_detectedTextHasBeenSet = true;
}

bool TextDetectionEn::DetectedTextHasBeenSet() const
{
    return m_detectedTextHasBeenSet;
}

int64_t TextDetectionEn::GetConfidence() const
{
    return m_confidence;
}

void TextDetectionEn::SetConfidence(const int64_t& _confidence)
{
    m_confidence = _confidence;
    m_confidenceHasBeenSet = true;
}

bool TextDetectionEn::ConfidenceHasBeenSet() const
{
    return m_confidenceHasBeenSet;
}

vector<Coord> TextDetectionEn::GetPolygon() const
{
    return m_polygon;
}

void TextDetectionEn::SetPolygon(const vector<Coord>& _polygon)
{
    m_polygon = _polygon;
    m_polygonHasBeenSet = true;
}

bool TextDetectionEn::PolygonHasBeenSet() const
{
    return m_polygonHasBeenSet;
}

string TextDetectionEn::GetAdvancedInfo() const
{
    return m_advancedInfo;
}

void TextDetectionEn::SetAdvancedInfo(const string& _advancedInfo)
{
    m_advancedInfo = _advancedInfo;
    m_advancedInfoHasBeenSet = true;
}

bool TextDetectionEn::AdvancedInfoHasBeenSet() const
{
    return m_advancedInfoHasBeenSet;
}

vector<WordCoordPoint> TextDetectionEn::GetWordCoordPoint() const
{
    return m_wordCoordPoint;
}

void TextDetectionEn::SetWordCoordPoint(const vector<WordCoordPoint>& _wordCoordPoint)
{
    m_wordCoordPoint = _wordCoordPoint;
    m_wordCoordPointHasBeenSet = true;
}

bool TextDetectionEn::WordCoordPointHasBeenSet() const
{
    return m_wordCoordPointHasBeenSet;
}

vector<CandWord> TextDetectionEn::GetCandWord() const
{
    return m_candWord;
}

void TextDetectionEn::SetCandWord(const vector<CandWord>& _candWord)
{
    m_candWord = _candWord;
    m_candWordHasBeenSet = true;
}

bool TextDetectionEn::CandWordHasBeenSet() const
{
    return m_candWordHasBeenSet;
}

vector<Words> TextDetectionEn::GetWords() const
{
    return m_words;
}

void TextDetectionEn::SetWords(const vector<Words>& _words)
{
    m_words = _words;
    m_wordsHasBeenSet = true;
}

bool TextDetectionEn::WordsHasBeenSet() const
{
    return m_wordsHasBeenSet;
}

