/* Copyright 2018 Streampunk Media Ltd.

  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.
*/

/* -LICENSE-START-
 ** Copyright (c) 2010 Blackmagic Design
 **
 ** Permission is hereby granted, free of charge, to any person or organization
 ** obtaining a copy of the software and accompanying documentation covered by
 ** this license (the "Software") to use, reproduce, display, distribute,
 ** execute, and transmit the Software, and to prepare derivative works of the
 ** Software, and to permit third-parties to whom the Software is furnished to
 ** do so, all subject to the following:
 **
 ** The copyright notices in the Software and this entire statement, including
 ** the above license grant, this restriction and the following disclaimer,
 ** must be included in all copies of the Software, in whole or in part, and
 ** all derivative works of the Software, unless such copies or derivative
 ** works are solely in the form of machine-executable object code generated by
 ** a source language processor.
 **
 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 ** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 ** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 ** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 ** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 ** DEALINGS IN THE SOFTWARE.
 ** -LICENSE-END-
 */

#ifndef TIMECODE_H
#define TIMECODE_H

#ifdef WIN32
#include <tchar.h>
#include <conio.h>
#include <objbase.h>		// Necessary for COM
#include <comdef.h>
#endif

#include <cstring>
#include <regex>
#include "macadam_util.h"
#include "node_api.h"
#include "DeckLinkAPI.h"

napi_value timecodeTest(napi_env env, napi_callback_info info);


struct frameTable {
  uint32_t dropFpMin;
  uint32_t dropFpMin10;
  uint32_t dropFpHour;
  uint32_t fpMinute;
  uint32_t fpHour;
  uint16_t scaledFps;

  frameTable(uint16_t fps) {
    scaledFps = (fps > 30) ? fps / 2 : fps;

    dropFpMin = (60 * scaledFps) - 2;
    dropFpMin10 = (10 * dropFpMin + 2);
    dropFpHour = 6 * dropFpMin10;

    fpMinute = 60 * scaledFps;
    fpHour = 60 * fpMinute;
  }
};

struct macadamTimecode : IDeckLinkTimecode {
  uint32_t value = 0;
  uint16_t fps;
  frameTable* frameTab = nullptr;
  BMDTimecodeFlags flags = bmdTimecodeFlagDefault;
  BMDTimecodeBCD GetBCD (void);
  BMDTimecodeUserBits usrBts = (BMDTimecodeUserBits) 0;
  macadamTimecode(
    uint16_t fps,
    bool drop = false,
    uint8_t hours = 0,
    uint8_t minutes = 0,
    uint8_t seconds = 0,
    uint8_t frames = 0,
    uint8_t framePair = 0);
  HRESULT GetComponents (
    /* out */ uint8_t *hours,
    /* out */ uint8_t *minutes,
    /* out */ uint8_t *seconds,
    /* out */ uint8_t *frames);
  HRESULT SetComponents (
    uint8_t hours,
    uint8_t minutes,
    uint8_t seconds,
    uint8_t frames,
    uint8_t framePair = 0);
  #ifdef WIN32
  HRESULT GetString (/* out */ BSTR *timecode);
  #elif __APPLE__
  HRESULT GetString (/* out */ CFStringRef *timecode);
  #else
  HRESULT GetString (/* out */ const char** timecode);
  #endif
  HRESULT formatTimecodeString(const char** timecode, bool fieldFlag = false);
  BMDTimecodeFlags GetFlags (void);
  HRESULT GetTimecodeUserBits (/* out */ BMDTimecodeUserBits *userBits);
  HRESULT SetTimecodeUserBits (BMDTimecodeUserBits userBits);
  void Update(void);
  HRESULT	QueryInterface (REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
  ULONG AddRef() { return 1; };
  ULONG Release() { return 1; };
  ~macadamTimecode() {
    if (frameTab != nullptr) { delete frameTab; }
  }
};

HRESULT parseTimecode(uint16_t fps, const char* tcstr, macadamTimecode** timecode);

#endif // TIMECODE_H
