#pragma once

/************************************************************************
    MeOS - Orienteering Software
    Copyright (C) 2009-2024 Melin Software HB

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Melin Software HB - software@melin.nu - www.melin.nu
    Eksoppsvägen 16, SE-75646 UPPSALA, Sweden

************************************************************************/

#include <vector>
#include "gdioutput.h"

class AutoCompleteHandler;

struct AutoCompleteRecord {
  AutoCompleteRecord() : id(-1) {}
  AutoCompleteRecord(const wstring &display, int prio, const wstring &name, int id) : display(display), prio(prio), name(name), id(id) {}
  wstring display;
  wstring name;
  int id;
  int prio = 0;
  bool operator<(const AutoCompleteRecord& rec) const {
    if (prio != rec.prio)
      return prio > rec.prio;
    else
      return display < rec.display;
  }
};

class AutoCompleteInfo {
public: 
  
private:

  AutoCompleteHandler *handler;
  HWND hWnd;
  
  string widgetId;
  bool lock;
  gdioutput &gdi;

  vector<AutoCompleteRecord> data;
  vector<pair<int, RECT>> rendered;
  
  bool modifedAutoComplete; // True if the user has made a selection
  int lastVisible;
  int currentIx;
public:
  AutoCompleteInfo(HWND hWnd, const string &widgetId, gdioutput &gdi);
  ~AutoCompleteInfo();
  bool matchKey(const string &k) const { return widgetId == k; }

  void destoy() {
    if (hWnd)
      DestroyWindow(hWnd);
    hWnd = nullptr;
  }

  void paint(HDC hDC);
  void setData(const vector<AutoCompleteRecord> &items);
  void click(int x, int y);
  void show();

  void upDown(int direction);
  void enter();
  
  bool locked() const { return lock; }
  const string &getTarget() const { return widgetId; }
  wstring getCurrent() const { if (size_t(currentIx) < data.size()) return data[currentIx].name; else return L""; }
  int getCurrentInt() const { if (size_t(currentIx) < data.size()) return data[currentIx].id; else return 0; }

  void setAutoCompleteHandler(AutoCompleteHandler *h) { handler = h; };
  static void registerAutoClass();
};

