#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Image.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_JPEG_Image.H>

#include <exception>
#include <iostream>
#include <string>

#include <sys/stat.h>
#include <dirent.h>

#include "src/fe/theme.hh"
#include "src/fe/window.hh"
#include "src/fe/widget.hh"
#include "src/fe/background.hh"
#include "src/fe/thumbnail.hh"

#include "src/uw/background.hh"

void thumbnail_cb(Fl_Widget *widget, void *userdata) {
  (void)widget;
  const char *filepath = static_cast<const char *>(userdata);
  if (filepath) {
    uw::Background b(filepath);
    b.setWallpaper(uw::Mode::TILE);
    fe::Widget::getSetButton()->deactivate();
  }
}

void usage(fe::Window w) {
  std::cout << w.getName() << "-" << w.getVersion() << '\n';
  std::cout << "usage: " << w.getName() << " " << w.getOptions() << " <image>"
    << std::endl;
}

int main(int argc, char **argv) {
  fe::Window w;

  if (argc == 1) {
    std::string tit = w.getName() + " " + w.getVersion();
    Fl_Window *window =
      new Fl_Window(w.getWidth(), w.getHeight(), tit.c_str());

    fe::Theme::SetDarkTheme();

    // ボタン
    fe::Widget::setChooseButton(new Fl_Button(10, 740, 200, 40, "画像を選択"));
    fe::Widget::setSetButton(new Fl_Button(230, 740, 200, 40, "背景の設置"));
    fe::Widget::getSetButton()->deactivate();

    // モード選択
    Fl_Choice *modeChoice = new Fl_Choice(450, 740, 200, 40, "");
    modeChoice->add("タイル");
    modeChoice->add("中央");
    modeChoice->add("全画像");
    modeChoice->value(0);
    modeChoice->align(FL_ALIGN_TOP);
    modeChoice->color(FL_BACKGROUND_COLOR);
    modeChoice->textcolor(FL_FOREGROUND_COLOR);
    modeChoice->selection_color(FL_BACKGROUND2_COLOR);
    fe::Widget::setModeChoice(modeChoice);

    // スクロールウィンドウ
    fe::Widget::setScroll(new Fl_Scroll(10, 10, 780, 720));
    fe::Widget::getScroll()->type(Fl_Scroll::VERTICAL);
    fe::Widget::getScroll()->begin();

    // サムネール
    fe::Thumbnail thumbnail;
    thumbnail.init();

    fe::Widget::getScroll()->end();

    // ボタンで選択
    fe::Widget::getChooseButton()->callback(fe::Background::chooseImageCb,
        (void *)fe::Widget::getSetButton());
    fe::Widget::getSetButton()->callback(fe::Background::setWallpaperCb);

    fe::Widget::getScroll()->end();
    window->end();
    window->show(argc, argv);

    return Fl::run();
  }

  if (argc < 3) {
    usage(w);
    return 1;
  }

  std::string modeStr = argv[1];
  std::string filename = argv[2];

  uw::Mode mode;

  if (modeStr == "-t") mode = uw::TILE;
  else if (modeStr == "-f") mode = uw::FILL;
  else if (modeStr == "-c") mode = uw::CENTER;
  else {
    usage(w);
    return 1;
  }

  try {
    uw::Background background(filename);

    if (!background.setWallpaper(mode)) {
      std::cerr << "背景を設定に失敗。" << std::endl;
      return 1;
    }
  } catch (const std::exception &e) {
    std::cerr << "エラー: " << e.what() << std::endl;
    return 1;
  }

  return 0;
}
