#pragma once
#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <vector>

using namespace std;

class Point
{
public: 
    int keyPointIndex;
    int x;
    int y;
public:
    Point() {}
    ~Point() {}
    bool jsonToObject(const rapidjson::Value& object){
        const auto end = object.MemberEnd();

        if(end == object.FindMember("keyPointIndex") || !object["keyPointIndex"].IsInt()){
            return false;
        }else{
            keyPointIndex = object["keyPointIndex"].GetInt();
        }
        if(end == object.FindMember("point") || !object["point"].IsObject()){
            return false;
        }else{
            const rapidjson::Value& obj = object["point"];
            const auto end1 = obj.MemberEnd();
            if(end1 == obj.FindMember("x") || !obj["x"].IsInt()){
                return false;
            }else{
                x = ((float)obj["x"].GetInt() / 574) * 1920;
            }
            if(end1 == obj.FindMember("y") || !obj["y"].IsInt()){
                return false;
            }else{
                y = ((float)obj["y"].GetInt() / 396) * 1080;
            }
        }
        return true;
    }
};

class InferRange
{
public:
    vector<Point> m_points;
public:
    InferRange() {}
    ~InferRange() {}

    bool jsonToObject(const std::string& json){
        rapidjson::Document doc;
        if (doc.Parse<rapidjson::kParseCommentsFlag>(json.c_str()).HasParseError()) {
            return false;
        }
        if(doc.IsArray())
        {
            const rapidjson::Value& objs = doc;
            for(int i = 0; i < (int)objs.Size(); i++){
                Point point;
                if(!point.jsonToObject(objs[i])){
                    return false;
                }
                m_points.push_back(point);
            }
        }else{
            return false;
        }
        return true;
    }
};