/*****************************************************************************
Open Source MIT License

Copyright (c) 2017 Matthew Ahmed

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/

#include "JpgFolders.h"

CJpgFolders::CJpgFolders(ZWin * pOwner, LPCTSTR sParentPath,
                         LPCTSTR sMatchParentPath)
  : ZWinUtil(pOwner)
{
  m_sOwnName = _T("Album folders array"); // used in SetError

  m_nAlbums = 0;
  _tcscpy(m_sParentPath, sParentPath);
  _tcscpy(m_sMatchParentPath, sMatchParentPath ? sMatchParentPath : _T(""));
}

CJpgFolders::~CJpgFolders(void)
{
  for (INT i = 0; i < m_nAlbums; i++)
  {
    DeleteAlbum(i);
  }

} // CJpgFolders::~CJpgFolders(void)

void CJpgFolders::DeleteAlbum(INT nAlbumIx)
{
  if (m_pAlbumsArr[nAlbumIx]) // Should always be TRUE
  {
    ALBUM * pAlbum = m_pAlbumsArr[nAlbumIx];
    if (pAlbum->sPathRel) delete pAlbum->sPathRel;
    if (pAlbum->sArt1) delete pAlbum->sArt1;
    if (pAlbum->sArtNml1) delete pAlbum->sArtNml1;
    if (pAlbum->sAlb2) delete pAlbum->sAlb2;
    if (pAlbum->sAlbNml2) delete pAlbum->sAlbNml2;
    if (pAlbum->sMatchPathRel) delete pAlbum->sMatchPathRel;
    // and finally the structure itself
    delete m_pAlbumsArr[nAlbumIx];
  }
}


INT CJpgFolders::AddAlbum(LPCTSTR sArtDir, LPCTSTR sAlbDir, INT nJpgBytes,
                  INT nMatchJpgBytes, LPCTSTR sMatchPathRel, INT nMatchLevel)
{
  // Allocate next unused member of array
  m_pAlbumsArr[m_nAlbums] = new ALBUM;
  if (!m_pAlbumsArr[m_nAlbums])
  {
    SetError(_T("Error allocating album structure"));
    return -1;
  }

  m_pAlbumsArr[m_nAlbums]->sPathRel = new TCHAR[MAX_PATH];
  m_pAlbumsArr[m_nAlbums]->sArt1 = new TCHAR[_MAX_DIR];
  m_pAlbumsArr[m_nAlbums]->sArtNml1 = new TCHAR[_MAX_DIR];
  m_pAlbumsArr[m_nAlbums]->sAlb2  = new TCHAR[_MAX_DIR];
  m_pAlbumsArr[m_nAlbums]->sAlbNml2 = new TCHAR[_MAX_DIR];
  m_pAlbumsArr[m_nAlbums]->sMatchPathRel = new TCHAR[MAX_PATH];


  if (!m_pAlbumsArr[m_nAlbums]->sPathRel ||
      !m_pAlbumsArr[m_nAlbums]->sArt1 ||
      !m_pAlbumsArr[m_nAlbums]->sArtNml1 ||
      !m_pAlbumsArr[m_nAlbums]->sAlb2 ||
      !m_pAlbumsArr[m_nAlbums]->sAlbNml2 ||
      !m_pAlbumsArr[m_nAlbums]->sMatchPathRel)
  {
    DeleteAlbum(m_nAlbums);
    SetError(_T("Error allocating strings for album structure"),
             sArtDir, sAlbDir);
    return -1;
  }

  _tcscpy(m_pAlbumsArr[m_nAlbums]->sArt1, sArtDir);
  _tcscpy(m_pAlbumsArr[m_nAlbums]->sAlb2, sAlbDir);
  _stprintf(m_pAlbumsArr[m_nAlbums]->sPathRel, _T("%s\\%s"), sArtDir, sAlbDir);
  _tcscpy(m_pAlbumsArr[m_nAlbums]->sMatchPathRel,
          sMatchPathRel ? sMatchPathRel : _T(""));

  m_pAlbumsArr[m_nAlbums]->nJpgBytes = nJpgBytes;
  m_pAlbumsArr[m_nAlbums]->nMatchJpgBytes = nMatchJpgBytes;
  m_pAlbumsArr[m_nAlbums]->nMatchLevel = nMatchLevel;

  // Normalized only filled in later if asked. Make "safe" for erroneous calls.
  m_pAlbumsArr[m_nAlbums]->sArtNml1[0] = _T('\0');
  m_pAlbumsArr[m_nAlbums]->sAlbNml2[0] = _T('\0');

  return m_nAlbums++; // Return index of new album (before ++)
} // INT CJpgFolders::AddAlbum(LPCTSTR sArtDir, LPCTSTR sAlbDir,
  //                           INT nMatchLevel, INT nJpgBytes)


BOOL CJpgFolders::SetNormalized(INT nHowMuch)
{
  for (INT i = 0; i < m_nAlbums; i++)
  {
    if (!Normalized(m_pAlbumsArr[i]->sArt1,
                    m_pAlbumsArr[i]->sArtNml1, nHowMuch) ||
        !Normalized(m_pAlbumsArr[i]->sAlb2,
                    m_pAlbumsArr[i]->sAlbNml2, nHowMuch))
      return FALSE;
  }
  return TRUE;
}

INT CJpgFolders::GetCount()
{
  return m_nAlbums;
}

void CJpgFolders::SetMatch(INT nAlbumIx, LPCTSTR sMatchPathRel, INT nMatchLevel)
{
  _tcscpy(m_pAlbumsArr[nAlbumIx]->sMatchPathRel, sMatchPathRel);
  m_pAlbumsArr[nAlbumIx]->nMatchLevel = nMatchLevel;
}

LPCTSTR CJpgFolders::GetPathRel(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->sPathRel;
}

LPCTSTR CJpgFolders::GetPathFull(INT nAlbumIx)
{
  static TCHAR sFull[MAX_PATH];
  _stprintf(sFull, _T("%s\\%s"), m_sParentPath, m_pAlbumsArr[nAlbumIx]->sPathRel);
  return sFull;
}

LPCTSTR CJpgFolders::GetArt1(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->sArt1;
}

LPCTSTR CJpgFolders::GetArtNml1(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->sArtNml1;
}

LPCTSTR CJpgFolders::GetAlb2(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->sAlb2;
}

LPCTSTR CJpgFolders::GetAlbNml2(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->sAlbNml2;
}

INT CJpgFolders::GetMatchLevel(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->nMatchLevel;
}

INT CJpgFolders::GetColorLevel(INT nAlbumIx)
{
  // Gray for multiple match levels, 5-7; return 5 if gray.
  return  min(m_pAlbumsArr[nAlbumIx]->nMatchLevel, 5);
}

LPCTSTR CJpgFolders::GetMatchPathRel(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->sMatchPathRel;
}

INT CJpgFolders::GetJpgBytes(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->nJpgBytes;
}

INT CJpgFolders::GetMatchJpgBytes(INT nAlbumIx)
{
  return m_pAlbumsArr[nAlbumIx]->nMatchJpgBytes;
}

LPCTSTR CJpgFolders::GetMatchPathFull(INT nAlbumIx)
{
  static TCHAR sFull[MAX_PATH];
  _stprintf(sFull, _T("%s\\%s"), m_sMatchParentPath,
            m_pAlbumsArr[nAlbumIx]->sMatchPathRel);
  return sFull;
}

LPCTSTR CJpgFolders::GetJpgPath(INT nAlbumIx, LPCTSTR sJpgName)
{
  static TCHAR sJpgPath[MAX_PATH];
  _stprintf(sJpgPath, _T("%s\\%s\\%s"),
            m_sParentPath, m_pAlbumsArr[nAlbumIx]->sPathRel, sJpgName);
  return sJpgPath;
}

LPCTSTR CJpgFolders::GetMatchJpgPath(INT nAlbumIx, LPCTSTR sJpgName)
{
  static TCHAR sJpgPath[MAX_PATH];
  _stprintf(sJpgPath, _T("%s\\%s\\%s"), m_sMatchParentPath,
            m_pAlbumsArr[nAlbumIx]->sMatchPathRel, sJpgName);
  return sJpgPath;
}

LPCTSTR CJpgFolders::GetParentPath()
{
  return m_sParentPath;
}

LPCTSTR CJpgFolders::GetMatchParentPath()
{
  return m_sMatchParentPath;
}



