#include <stdio.h>
#include <stdlib.h>

#include "kernel.h"
#include "swis.h"
#include "bbc.h"

#include "toolbox.h"
#include "flex.h"
#include "wimp.h"
#include "wimplib.h"
#include "drawfile.h"
#include "debug.h"

#include "msgtrans.h"
#include "pinboard.h"
#include "watermark.h"

void watermark_redraw(_backdrop_state *state,WimpRedrawWindowBlock *block)
{
  Transform t;


  t[0][0]=65536;
  t[0][1]=0;
  t[1][0]=0;
  t[1][1]=65536;
  t[2][0]=state->watermark_x_xform;
  t[2][1]=state->watermark_y_xform;

  drawfile_render(0,state->watermark_data,state->watermark_length,&t,0/*&block->redraw_area*/,0);
}

void watermark_recalc_plot(_backdrop_state *state)
{
  BBox extent;
  int w,h;

  // recalculate the bbox for the plot area
  // so we don't have to do it every redraw

  if (state->watermark_data==NULL) return;

  // get the drawfile's bbox
  drawfile_bbox(0,state->watermark_data,state->watermark_length,NULL,&extent);

  // make into OS units
  w=(extent.xmax-extent.xmin)/256;
  h=(extent.ymax-extent.ymin)/256;
  
  // determine plot location
  switch (state->watermark_location) {
    case WATERMARK_TOPLEFT:
      state->watermark_plot_area.xmin=state->wm_x_offset;
      state->watermark_plot_area.xmax=state->watermark_plot_area.xmin+w;
      state->watermark_plot_area.ymax=state->window_height+iconbar_height-state->wm_y_offset;
      state->watermark_plot_area.ymin=state->watermark_plot_area.ymax-h;
      break;

    case WATERMARK_TOPCENTRE:
      state->watermark_plot_area.xmin=(state->window_width-w)/2;
      state->watermark_plot_area.xmax=state->watermark_plot_area.xmin+w;
      state->watermark_plot_area.ymax=state->window_height+iconbar_height-state->wm_y_offset;
      state->watermark_plot_area.ymin=state->watermark_plot_area.ymax-h;
      break;

    case WATERMARK_TOPRIGHT:
      state->watermark_plot_area.xmax=state->window_width-state->wm_x_offset;
      state->watermark_plot_area.xmin=state->watermark_plot_area.xmax-w;
      state->watermark_plot_area.ymax=state->window_height+iconbar_height-state->wm_y_offset;
      state->watermark_plot_area.ymin=state->watermark_plot_area.ymax-h;
      break;

    case WATERMARK_LEFT:
      state->watermark_plot_area.xmin=state->wm_x_offset;
      state->watermark_plot_area.xmax=state->watermark_plot_area.xmin+w;
      state->watermark_plot_area.ymin=(state->window_height-h)/2+iconbar_height;
      state->watermark_plot_area.ymax=state->watermark_plot_area.ymin+h;
      break;

    case WATERMARK_CENTRE:
      state->watermark_plot_area.xmin=(state->window_width-w)/2;
      state->watermark_plot_area.xmax=state->watermark_plot_area.xmin+w;
      state->watermark_plot_area.ymin=(state->window_height-h)/2+iconbar_height;
      state->watermark_plot_area.ymax=state->watermark_plot_area.ymin+h;
      break;

    case WATERMARK_RIGHT:
      state->watermark_plot_area.xmax=state->window_width-state->wm_x_offset;
      state->watermark_plot_area.xmin=state->watermark_plot_area.xmax-w;
      state->watermark_plot_area.ymin=(state->window_height-h)/2+iconbar_height;
      state->watermark_plot_area.ymax=state->watermark_plot_area.ymin+h;
      break;

    case WATERMARK_BOTTOMLEFT:
      state->watermark_plot_area.xmin=state->wm_x_offset;
      state->watermark_plot_area.xmax=state->watermark_plot_area.xmin+w;
      state->watermark_plot_area.ymin=iconbar_height+state->wm_y_offset;
      state->watermark_plot_area.ymax=state->watermark_plot_area.ymin+h;
      break;

    case WATERMARK_BOTTOMCENTRE:
      state->watermark_plot_area.xmin=(state->window_width-w)/2;
      state->watermark_plot_area.xmax=state->watermark_plot_area.xmin+w;
      state->watermark_plot_area.ymin=iconbar_height+state->wm_y_offset;
      state->watermark_plot_area.ymax=state->watermark_plot_area.ymin+h;
      break;

    case WATERMARK_BOTTOMRIGHT:
      state->watermark_plot_area.xmax=state->window_width-state->wm_x_offset;
      state->watermark_plot_area.xmin=state->watermark_plot_area.xmax-w;
      state->watermark_plot_area.ymin=iconbar_height+state->wm_y_offset;
      state->watermark_plot_area.ymax=state->watermark_plot_area.ymin+h;
     break;
  }

  // determine plot offsets to get to the plot area
  state->watermark_x_xform=state->watermark_plot_area.xmin*256-extent.xmin;
  state->watermark_y_xform=state->watermark_plot_area.ymin*256-extent.ymin;
}

void watermark_load(_backdrop_state *state,char *filename)
{
  _kernel_oserror *e;
  int type,length,filetype;

  if (state->watermark_data) {
    flex_free((flex_ptr)&state->watermark_data);
    state->watermark_data=NULL;
    state->watermark_length=0;
  }

  if (filename[0]==0) return;

  e=_swix(OS_File,_INR(0,1)|_OUT(0)|_OUT(4)|_OUT(6),23,filename,&type,&length,&filetype);
  if (e) {
    wimp_report_error(e,1,msgtrans_lookup("_TaskName"),NULL,NULL,NULL);
    return;
  }

  if (type!=1) {
    msgtrans_report_error("WaterNF");
    return;
  }

  if (filetype!=0xaff) {
    msgtrans_report_error("WaterBad");
    return;
  }

  if (!flex_alloc((flex_ptr)&state->watermark_data,length)) {
    msgtrans_report_error("NoMem");
    return;
  }
  e=_swix(OS_File,_INR(0,3),255,filename,state->watermark_data,0);

  if (e) {
    flex_free((flex_ptr)&state->watermark_data);
    state->watermark_data=NULL;
    state->watermark_length=0;
    return;
  }
  state->watermark_length=length;

  watermark_recalc_plot(state);
}
