// Copyright (c) 2022 Dominic Masters
// 
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#pragma once
#include "../Asset.hpp"
#include "asset/AssetLoader.hpp"
#include "display/Texture.hpp"

namespace Dawn {
  enum TextureAssetHeaderParseState {
    TEXTURE_ASSET_HEADER_PARSE_STATE_VERSION,
    TEXTURE_ASSET_HEADER_PARSE_STATE_WIDTH,
    TEXTURE_ASSET_HEADER_PARSE_STATE_HEIGHT,
    TEXTURE_ASSET_HEADER_PARSE_STATE_FORMAT,
    TEXTURE_ASSET_HEADER_PARSE_STATE_WRAP_MODE_X,
    TEXTURE_ASSET_HEADER_PARSE_STATE_WRAP_MODE_Y,
    TEXTURE_ASSET_HEADER_PARSE_STATE_FILTER_MODE_MIN,
    TEXTURE_ASSET_HEADER_PARSE_STATE_FILTER_MODE_MAG,
    TEXTURE_ASSET_HEADER_PARSE_STATE_END
  };

  enum TextureAssetLoadState {
    TEXTURE_ASSET_LOAD_STATE_INITIAL,
    TEXTURE_ASSET_LOAD_STATE_OPENING,
    TEXTURE_ASSET_LOAD_STATE_PARSING_HEADER,
    TEXTURE_ASSET_LOAD_STATE_SYNC_HANDOFF,
    TEXTURE_ASSET_LOAD_STATE_BUFFERING_TEXTURE,
    TEXTURE_ASSET_LOAD_STATE_COMPLETE,
  };

  class TextureAsset : public Asset {
    protected:
      AssetLoader loader;
      uint8_t *buffer = nullptr;
      int32_t width = -1, height = -1;
      uint8_t *colors;
      enum TextureAssetLoadState state = TEXTURE_ASSET_LOAD_STATE_INITIAL;
      enum TextureFormat format;
      enum TextureWrapMode wrapModeX;
      enum TextureWrapMode wrapModeY;
      enum TextureFilterMode filterModeMin;
      enum TextureFilterMode filterModeMag;

    public:
      Texture texture;

      /**
       * Constructs a texture asset loader. You should instead use the parent
       * asset managers' abstracted load method
       * 
       * @param name File name asset to load, omitting the extension.
       */
      TextureAsset(const std::string name);

      void updateSync() override;
      void updateAsync() override;

      /**
       * Dispose / Cleanup the texture asset. Will also dispose the underlying
       * texture itself.
       */
      ~TextureAsset();
  };
}