//
// Created by Иван_Архипов on 31.10.2017.
//

#include "BinaryData.h"
#include "Common/DatException.h"
#include "Common/ZLib/zlib.h"
#include <algorithm>

extern  "C++"
{
namespace LOTRO_DAT {

    BinaryData::BinaryData() {
        data_ = nullptr;
        size_ = 0;
    }

    BinaryData::BinaryData(const BinaryData &d) {
        size_ = d.size_;
        data_ = new unsigned char[size_];
        memcpy(data_, d.data_, size_);
    }

    BinaryData::BinaryData(char* data, unsigned int size) {
        size_ = size;
        data_ = new unsigned char[size_];
        memcpy(data_, data, size_);
    }

    BinaryData::BinaryData(unsigned int size) {
        data_ = new unsigned char[size];
        size_ = size;
    }

    BinaryData::~BinaryData() {
        if (size_ != 0 && data_ != nullptr)
            delete[] data_;
    }

    unsigned char&  BinaryData::operator[](const unsigned int &pos) {
        if (pos >= size_)
            throw DatException("Bad BinaryData::operator[]. Position is out of range.");
        return data_[pos];
    }

    BinaryData BinaryData::operator +(const BinaryData &b) {
        BinaryData res(size_ + b.size());
        if (size_ > 0)
            memcpy(res.data_, data_, size_);
        if (b.size() > 0)
            memcpy(res.data_ + size_, b.data_, b.size_);
        return res;
    }

    // Translates T bytes from data into number using UTF-16LE encoding of the .dat file
    template<int T>
    long long BinaryData::ToNumber(const long long &pos) const {
        try {
            long long ans = 0;

            if (pos + T > size_)
                throw DatException("Bad BinaryData::ToNumber(). Reached end of BinaryData!", DATA_EXCEPTION);

            for (int i = T - 1; i >= 0; i--)
                ans = ((ans << 8ll) | data_[pos + i]);

            return ans;
        } catch (...) {
            throw DatException("Bad BinaryData::ToNumber(). Error while using template function", DATA_EXCEPTION);
        }
    }

    // Translates T bytes from data into number in raw format
    template<int T>
    long long BinaryData::ToNumberRAW(const long long &pos) const {
        try {
            long long ans = 0;

            if (pos + T >= size_)
                throw DatException("Bad BinaryData::ToNumber(). Reached end of BinaryData!", DATA_EXCEPTION);

            for (int i = 0; i < T; i++)
                ans = ((ans << 8ll) | data_[pos + i]);

            return ans;
        } catch (...) {
            throw DatException("Bad BinaryData::ToNumber(). Error while using template function", DATA_EXCEPTION);
        }
    }

    // Makes data from specified T bytes of number in Little Endian encoding
    template<int T>
    void BinaryData::FromNumber(const long long &number) {
        if (T < 0)
            throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes < 0");
        try {
            delete[] data_;

            size_ = size_t(T);
            data_ = new unsigned char[size_];

            for (size_t i = 0; i < size_; i++)
            {
                data_[i] = (unsigned char)((number >> (8 * i)) & 0xFF);
            }

        } catch (...) {
            throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
        }
    }

    // Makes data from specified T bytes of number in raw
    template<int T>
    void BinaryData::FromNumberRAW(const long long &number) {
        if (T <= 0)
            throw DatException("Bad BinaryData::FromNumber() - trying to make data from amount of bytes <= 0");
        try {
            FromNumber<T>(number);
            std::reverse(data_, data_ + size());
        } catch (...) {
            throw DatException("Bad BinaryData::ToNumber(). Error in using template function", DATA_EXCEPTION);
        }
    }

    BinaryData &BinaryData::operator=(const BinaryData &data) {
        if (&data == this)
            return *this;

        if (size_ != 0 && data_ != nullptr)
            delete[] data_;

        size_ = data.size_;
        data_ = nullptr;

        if (size_ != 0) {
            data_ = new unsigned char[size_];
            memcpy(data_, data.data_, size_);
        }

        return *this;
    }

    size_t BinaryData::size() const {
        return size_;
    }

    unsigned char *BinaryData::data() const {
        return data_;
    }

    bool BinaryData::WriteToFile(const char *filename) const {
        FILE *f;
        fopen_s(&f, filename, "wb");
        if (f == nullptr) {
            throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
        }

        fwrite(data(), size(), 1, f);
        fclose(f);
        return true;
    }

    bool BinaryData::WriteToFile(const std::string &filename) const {
        return WriteToFile(filename.c_str());
    }


    void BinaryData::ReadFromFile(const char *filename) {
        FILE *f;
        fopen_s(&f, filename, "rb");
        if (f == nullptr) {
            throw DatException("Bad BinaryData::WriteToFile() - unable to open output file", EXPORT_EXCEPTION);
        }

        _fseeki64(f, 0, SEEK_END);
        long long file_size = ftell(f);
        _fseeki64(f, 0, SEEK_SET);

        BinaryData temp_data = BinaryData(unsigned(file_size));

        fread(temp_data.data_, temp_data.size_, 1, f);
        *this = temp_data;
        fclose(f);
    }

    void BinaryData::ReadFromFile(const std::string &filename) {
        ReadFromFile(filename.c_str());
    }

    bool BinaryData::CheckCompression() const {
        if (size() < 10)
            return false;
        auto header = ToNumberRAW<2>(4);
        return (header == 0x7801 || header == 0x789C || header == 0x78DA);
    }

    BinaryData BinaryData::DecompressData(unsigned int offset) const {
        const unsigned max_unpacked_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum unpacked data;

        BinaryData decompressed(max_unpacked_size);

        uLongf new_size = max_unpacked_size;
        int res = uncompress(decompressed.data_, &new_size, data_ + offset, size_ - offset);

        if (res != 0) {
            throw DatException("Bad BinaryData::DecompressData() - uncompress() failed!", DATA_EXCEPTION);
        }

        decompressed.size_ = (unsigned int)new_size;
        return decompressed;
    }

    BinaryData BinaryData::CompressData(unsigned int offset) const {
        const unsigned max_file_size = 1024 * 1024 * 40; // Setting 40 MB as a maximum packed data;

        BinaryData compressed(max_file_size);

        uLongf new_size = max_file_size;
        int res = compress2(compressed.data_, &new_size, data_ + offset, size_ - offset, 9);

        if (res != 0) {
            throw DatException("Bad BinaryData::CompressData() - compress failed!", DATA_EXCEPTION);
        }

        compressed.size_ = (unsigned int)new_size;
        return compressed;
    }

    BinaryData BinaryData::CutData(long long first, long long last) const {
        if (last < 0)
            last = size();

        if (last > size())
            throw DatException("Bad BinaryData::CutData() - parameter 'last' is out of range");

        BinaryData newdata(unsigned(last - first));
        memcpy(newdata.data(), data() + first, newdata.size());
        return newdata;
    }

    bool BinaryData::operator==(const BinaryData &b) const {
        if (size() != b.size())
            return false;
        for (int i = 0; i < size(); i++)
            if (data_[i] != b.data_[i])
                return false;
        return true;
    }

    bool BinaryData::operator!=(const BinaryData &b) const {
        return !(*this == b);
    }


    template long long BinaryData::ToNumber<1>(long long const&) const;
    template long long BinaryData::ToNumber<2>(long long const&) const;
    template long long BinaryData::ToNumber<4>(long long const&) const;
    template long long BinaryData::ToNumber<8>(long long const&) const;

    template void BinaryData::FromNumber<1>(const long long &);
    template void BinaryData::FromNumber<2>(const long long &);
    template void BinaryData::FromNumber<4>(const long long &);
    template void BinaryData::FromNumber<8>(const long long &);

    template long long BinaryData::ToNumberRAW<1>(long long const&) const;
    template long long BinaryData::ToNumberRAW<2>(long long const&) const;
    template long long BinaryData::ToNumberRAW<4>(long long const&) const;
    template long long BinaryData::ToNumberRAW<8>(long long const&) const;

    template void BinaryData::FromNumberRAW<1>(const long long &);
    template void BinaryData::FromNumberRAW<2>(const long long &);
    template void BinaryData::FromNumberRAW<4>(const long long &);
    template void BinaryData::FromNumberRAW<8>(const long long &);
}
}