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

#include "BinaryData.h"
#include "DatException.h"
#include "zlib/zlib.h"

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(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];
    }

    // 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 {
            if (size_ != 0 && data_ != nullptr)
                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 {
            if (size_ != 0 && data_ != nullptr)
                delete[] data_;

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

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

        } 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());
	}

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

    void BinaryData::DecompressData(unsigned int offset) {
        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;
		*this = decompressed;
		return;
	}

    BinaryData BinaryData::CutData(long long first, long long last) {
        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;
    }

    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 &);

}
}