/*------------------------------------------------------------------------------
 src/bin/djview/windowFile.cxx

 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

//#define DJ_DBG
#include "window.h"
#include "widget.h"
#include "app.h"
#include <djMenuWidget.h>
#include <djFileBrowser.h>

namespace djview {

void Window::fileOpen(const Str & in, bool init)
{
  DJ_DBG_FNC("Window::fileOpen");
  DJ_DBG_PRN("in = " << in);
  DJ_DBG_PRN("init = " << init);
  DJ_DBG_PRN("frame = " << _playbackFrame);

  if (_imageLoad) {
    delete _imageLoad;
    _imageLoad = 0;
  }

  _fileName = in;
  _playbackFrameStart = 0;
  _playbackFrameEnd = 0;
  if (! init) {
    _playbackIn = false;
    _playbackOut = false;
  }

  if (! _fileName.isNull()) {
    ImageIoInfo info;
    _imageLoad = imageLoad(_fileName, &info);
    if (! _imageLoad) {
      djviewApp->error(
        Str("Cannot open \"%1\".").arg(FileInfo(in).get(-1, false))
      );
    } else {
      if (! init) djviewApp->fileRecentList(_fileName);
      _playbackFrameStart = info.timeStart();
      _playbackFrameEnd = info.timeEnd();
      if (! init) {
        _playbackFrame = info.timeStart();
        if (djviewApp->playbackFileSpeed())
          _playbackSpeed = info.timeSpeed();
      }
    }
  }

  title(
    (! _fileName.isNull() && _imageLoad) ?
    Str("%1 - %2").arg(djviewApp->name()).arg(_fileName) :
    djviewApp->name()
  );
  playbackUpdate();
  playbackTimeUpdate();
  playbackSpeedUpdate();
  playbackFrameUpdate();
  menuUpdate();
  imageUpdate();

  if (! init && djviewApp->windowFileFit()) {
    if (fullscreen()) _viewWidget->viewFit();
    else windowFit();
  }

  if (! init)
    playback(
      (
        ! _fileName.isNull() &&
        (_playbackFrameStart != _playbackFrameEnd) &&
        djviewApp->playbackFile()
      ) ?
        PLAYBACK_FORWARD :
        PLAYBACK_STOP
    );
}

void Window::fileOpenCallback(const Str & in)
{
  if (! _callbacks) return;
  fileOpen(in);
}

void Window::fileOpenCallback(bool)
{
  if (! _callbacks) return;
  fileBrowser(this, fileOpenCallback);
}

void Window::fileOpenCallback() { fileOpenCallback(true); }

void Window::fileRecentListUpdate()
{
  menuUpdate();
}

void Window::fileRecentListCallback()
{
  if (! _callbacks) return;
  fileOpen(
    djviewApp->fileRecentList()[dj::find(_fileMenuRecentId, _menubar->value())]
  );
}

void Window::fileReload()
{  
  DJ_DBG_FNC("Window::fileReload");

  if (_imageLoad) {
    delete _imageLoad;
    _imageLoad = 0;
  }
  if (! _fileName.isNull())
    _imageLoad = imageLoad(_fileName);
  imageUpdate();
}

void Window::fileReloadCallback()
{
  if (! _callbacks) return;
  djviewApp->fileReload(_fileName);
}

void Window::fileCloseCallback(bool)
{
  if (! _callbacks) return;
  fileOpen("");
}

void Window::fileCloseCallback() { fileCloseCallback(true); }

void Window::fileProxy(int in)
{
  if (in == _fileProxy) return;
  
  DJ_DBG_FNC("Window::fileProxy");
  DJ_DBG_PRN("in = " << in);
  
  _fileProxy = in;
  imageUpdate();
  menuUpdate();
}

void Window::fileProxyCallback()
{
  if (! _callbacks) return;
  fileProxy(dj::find(_fileMenuProxyId, _menubar->value()));
}

void Window::filePixelFormat(int in)
{
  if (in == _filePixelFormat) return;

  DJ_DBG_FNC("Window::filePixelFormat");
  DJ_DBG_PRN("in = " << in);

  _filePixelFormat = in;
  imageUpdate();
  menuUpdate();
}

void Window::filePixelFormatCallback()
{
  if (! _callbacks) return;
  filePixelFormat(dj::find(_fileMenuPixelFormatId, _menubar->value()));
}

void Window::filePixelType(int in)
{
  if (in == _filePixelType) return;

  DJ_DBG_FNC("Window::filePixelType");
  DJ_DBG_PRN("in = " << in);
  
  _filePixelType = in;
  imageUpdate();
  menuUpdate();
}

void Window::filePixelTypeCallback()
{
  if (! _callbacks) return;
  filePixelType(dj::find(_fileMenuPixelTypeId, _menubar->value()));
}

}
