/*
    wmimagedock

  Copyright (c) 2018 Michael Heras

  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 2 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, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307,
  USA.
*/
#include <iostream>
#include <Imlib2.h>
#include "imagefiles.hpp"
#include "setBG.hpp"

void setBG()
{
    Display *display;

    display = XOpenDisplay( NULL );

     if (!display){
        std::cout<<"No display setBG"<<std::endl;
        //exit(1);
        return;
    }

	Visual *vis;
	Colormap cm;
	Pixmap pixmap;
	Imlib_Image buffer;
	Imlib_Image image;
//	Imlib_Color_Range range;
	Imlib_Context context;
	Imlib_Color_Modifier modifier;
    modifier = NULL;
    context = imlib_context_new();
    imlib_context_push(context);
    imlib_context_set_display(display);

    vis = DefaultVisual(display, 0);
    cm = DefaultColormap(display, 0);

    int screenW = DisplayWidth(display, 0);
    int screenH = DisplayHeight(display, 0);
    int depth = DefaultDepth(display, 0);

    pixmap = XCreatePixmap(display, RootWindow(display, 0), screenW, screenH, depth);

    imlib_context_set_display(display);
    imlib_context_set_visual(vis);
    imlib_context_set_colormap(cm);
    imlib_context_set_drawable(pixmap);
    imlib_context_set_color_range(imlib_create_color_range());

    buffer = imlib_create_image(screenW, screenH);
    //set buffer
     imlib_context_set_image(buffer);


    imlib_context_set_color(0, 0, 0, 255);
    imlib_image_fill_rectangle(0, 0, screenW, screenH);
    imlib_context_set_dither(1);
    imlib_context_set_blend(1);
    modifier = imlib_create_color_modifier();
    imlib_context_set_color_modifier(modifier);

    //load image here
    std::string fn = opts.dfile.at( opts.leftoff );
    image = imlib_load_image_immediately_without_cache(fn.c_str());
    imlib_context_set_image(image);
    int w = imlib_image_get_width();
    int h = imlib_image_get_height();
    imlib_context_set_image(buffer);
    /*
    double aspect = (double) screenH / (double) h;
    int top = (screenH - (int) (h * aspect)) / 2;
    int left = (screenW - (int) (w * aspect)) / 2;
    imlib_blend_image_onto_image (image, 0, 0, 0, w, h, left, top,
                                    get_adjusted_by_aspect(get_aspect(h,screenH),w),
                                    get_adjusted_by_aspect(get_aspect(h,screenH),h));
    */
    imlib_blend_image_onto_image (image, 0, 0, 0,w, h, 0,0, screenW, screenH);

 if (modifier != NULL) {
        imlib_context_set_color_modifier(modifier);
        imlib_apply_color_modifier();
        imlib_free_color_modifier();
        modifier = NULL;
      }

    imlib_render_image_on_drawable(0, 0);
    imlib_free_image_and_decache ();
    imlib_free_color_range();
    XKillClient (display, AllTemporary);
    XSetCloseDownMode (display, RetainTemporary);

    XSetWindowBackgroundPixmap (display, RootWindow (display, 0), pixmap);
    XClearWindow (display, RootWindow (display, 0));
    XFlush (display);
    XSync (display, False);
    imlib_context_pop();
    imlib_context_free(context);
}

void setBG(std::string fn)
{
    Display *display;

    display = XOpenDisplay( NULL );

     if (!display){
        std::cout<<"No display setBG"<<std::endl;
        //exit(1);
        return;
    }

	Visual *vis;
	Colormap cm;
	Pixmap pixmap;
	Imlib_Image buffer;
	Imlib_Image image;
//	Imlib_Color_Range range;
	Imlib_Context context;
	Imlib_Color_Modifier modifier;
    modifier = NULL;
    context = imlib_context_new();
    imlib_context_push(context);
    imlib_context_set_display(display);

    vis = DefaultVisual(display, 0);
    cm = DefaultColormap(display, 0);

    int screenW = DisplayWidth(display, 0);
    int screenH = DisplayHeight(display, 0);
    int depth = DefaultDepth(display, 0);

    pixmap = XCreatePixmap(display, RootWindow(display, 0), screenW, screenH, depth);

    imlib_context_set_display(display);
    imlib_context_set_visual(vis);
    imlib_context_set_colormap(cm);
    imlib_context_set_drawable(pixmap);
    imlib_context_set_color_range(imlib_create_color_range());

    buffer = imlib_create_image(screenW, screenH);
    //set buffer
     imlib_context_set_image(buffer);


    imlib_context_set_color(0, 0, 0, 255);
    imlib_image_fill_rectangle(0, 0, screenW, screenH);
    imlib_context_set_dither(1);
    imlib_context_set_blend(1);
    modifier = imlib_create_color_modifier();
    imlib_context_set_color_modifier(modifier);

    //load image here
  //  std::string fn = opts.dfile.at( opts.leftoff );
    image = imlib_load_image_immediately_without_cache(fn.c_str());
    imlib_context_set_image(image);
    int w = imlib_image_get_width();
    int h = imlib_image_get_height();
    imlib_context_set_image(buffer);

    /*
    double aspect = (double) screenH / (double) h;

    int top = (screenH - (int) (h * aspect)) / 2;
    int left = (screenW - (int) (w * aspect)) / 2;
    imlib_blend_image_onto_image (image, 0, 0, 0, w, h, left, top,
                                    get_adjusted_by_aspect(get_aspect(h, screenH),w),
                                    get_adjusted_by_aspect(get_aspect(h, screenH),h));
    */

    imlib_blend_image_onto_image (image, 0, 0, 0,w, h, 0,0, screenW, screenH);

 if (modifier != NULL) {
        imlib_context_set_color_modifier(modifier);
        imlib_apply_color_modifier();
        imlib_free_color_modifier();
        modifier = NULL;
      }

    imlib_render_image_on_drawable(0, 0);
    imlib_free_image_and_decache ();
    imlib_free_color_range();
    XKillClient (display, AllTemporary);
    XSetCloseDownMode (display, RetainTemporary);

    XSetWindowBackgroundPixmap (display, RootWindow (display, 0), pixmap);
    XClearWindow (display, RootWindow (display, 0));
    XFlush (display);
    XSync (display, False);
    imlib_context_pop();
    imlib_context_free(context);
}


void setBGTile()
{
    Display *display;

    display = XOpenDisplay( NULL );

     if (!display){
        std::cout<<"No display setBG"<<std::endl;
        //exit(1);
        return;
    }

	Visual *vis;
	Colormap cm;
	Pixmap pixmap;
	Imlib_Image buffer;
	Imlib_Image image;
//	Imlib_Color_Range range;
	Imlib_Context context;
	Imlib_Color_Modifier modifier;
    modifier = NULL;
    context = imlib_context_new();
    imlib_context_push(context);
    imlib_context_set_display(display);

    vis = DefaultVisual(display, 0);
    cm = DefaultColormap(display, 0);

    int newW, newH,top,left, x, y;
    int screenW = DisplayWidth(display, 0);
    int screenH = DisplayHeight(display, 0);
    int depth = DefaultDepth(display, 0);

    pixmap = XCreatePixmap(display, RootWindow(display, 0), screenW, screenH, depth);

    imlib_context_set_display(display);
    imlib_context_set_visual(vis);
    imlib_context_set_colormap(cm);
    imlib_context_set_drawable(pixmap);
    imlib_context_set_color_range(imlib_create_color_range());

    buffer = imlib_create_image(screenW, screenH);
    //set buffer
     imlib_context_set_image(buffer);


    imlib_context_set_color(0, 0, 0, 255);
    imlib_image_fill_rectangle(0, 0, screenW, screenH);
    imlib_context_set_dither(1);
    imlib_context_set_blend(1);
    modifier = imlib_create_color_modifier();
    imlib_context_set_color_modifier(modifier);

    //load image here
    std::string fn = opts.dfile.at( opts.leftoff );
    image = imlib_load_image_immediately_without_cache(fn.c_str());
    imlib_context_set_image(image);
    int w = imlib_image_get_width();
    int h = imlib_image_get_height();
    imlib_context_set_image(buffer);
    /*
    double aspect = (double) screenH / (double) h;
    int top = (screenH - (int) (h * aspect)) / 2;
    int left = (screenW - (int) (w * aspect)) / 2;
    imlib_blend_image_onto_image (image, 0, 0, 0, w, h, left, top,
                                    get_adjusted_by_aspect(get_aspect(h,screenH),w),
                                    get_adjusted_by_aspect(get_aspect(h,screenH),h));

    imlib_blend_image_onto_image (image, 0, 0, 0,w, h, 0,0, screenW, screenH);
    */

    if ( h > (screenH / 3))
    {
        newH = (get_adjusted_by_aspect(get_aspect(h,screenH),h) / 3);
        newW = (get_adjusted_by_aspect(get_aspect(h,screenH),w) / 3);
    }
    else
    {
        newH = h;
        newW = w;
    }

    top = (screenH - (int) (h * get_aspect(h,screenH))) / 2;
    left = (screenW - (int) (w * get_aspect(h,screenH))) / 2;

    for (; left > 0; left -= newW)
    {}
        for (; top > 0; top -= newH)
        {}

    for (x = left; x < screenW; x += newW) {
        for (y = top; y < screenH; y += newH)
            imlib_blend_image_onto_image (image, 0, 0, 0, w, h, x, y, newW, newH);
        }

 if (modifier != NULL) {
        imlib_context_set_color_modifier(modifier);
        imlib_apply_color_modifier();
        imlib_free_color_modifier();
        modifier = NULL;
      }

    imlib_render_image_on_drawable(0, 0);
    imlib_free_image_and_decache ();
    imlib_free_color_range();
    XKillClient (display, AllTemporary);
    XSetCloseDownMode (display, RetainTemporary);

    XSetWindowBackgroundPixmap (display, RootWindow (display, 0), pixmap);
    XClearWindow (display, RootWindow (display, 0));
    XFlush (display);
    XSync (display, False);
    imlib_context_pop();
    imlib_context_free(context);
}

void setBGTile(std::string fn)
{
    Display *display;

    display = XOpenDisplay( NULL );

     if (!display){
        std::cout<<"No display setBG"<<std::endl;
        //exit(1);
        return;
    }

	Visual *vis;
	Colormap cm;
	Pixmap pixmap;
	Imlib_Image buffer;
	Imlib_Image image;
//	Imlib_Color_Range range;
	Imlib_Context context;
	Imlib_Color_Modifier modifier;
    modifier = NULL;
    context = imlib_context_new();
    imlib_context_push(context);
    imlib_context_set_display(display);

    vis = DefaultVisual(display, 0);
    cm = DefaultColormap(display, 0);

    int newW, newH, top, left, x, y;
    int screenW = DisplayWidth(display, 0);
    int screenH = DisplayHeight(display, 0);
    int depth = DefaultDepth(display, 0);

    pixmap = XCreatePixmap(display, RootWindow(display, 0), screenW, screenH, depth);

    imlib_context_set_display(display);
    imlib_context_set_visual(vis);
    imlib_context_set_colormap(cm);
    imlib_context_set_drawable(pixmap);
    imlib_context_set_color_range(imlib_create_color_range());

    buffer = imlib_create_image(screenW, screenH);
    //set buffer
     imlib_context_set_image(buffer);


    imlib_context_set_color(0, 0, 0, 255);
    imlib_image_fill_rectangle(0, 0, screenW, screenH);
    imlib_context_set_dither(1);
    imlib_context_set_blend(1);
    modifier = imlib_create_color_modifier();
    imlib_context_set_color_modifier(modifier);

    //load image here
  //  std::string fn = opts.dfile.at( opts.leftoff );
    image = imlib_load_image_immediately_without_cache(fn.c_str());
    imlib_context_set_image(image);
    int w = imlib_image_get_width();
    int h = imlib_image_get_height();
    imlib_context_set_image(buffer);

    /*
    double aspect = (double) screenH / (double) h;

    int top = (screenH - (int) (h * aspect)) / 2;
    int left = (screenW - (int) (w * aspect)) / 2;
    imlib_blend_image_onto_image (image, 0, 0, 0, w, h, left, top,
                                    get_adjusted_by_aspect(get_aspect(h, screenH),w),
                                    get_adjusted_by_aspect(get_aspect(h, screenH),h));


    imlib_blend_image_onto_image (image, 0, 0, 0,w, h, 0,0, screenW, screenH);
    */

    if ( h > (screenH / 3))
    {
        newH = (get_adjusted_by_aspect(get_aspect(h,screenH),h) / 3);
        newW = (get_adjusted_by_aspect(get_aspect(h,screenH),w) / 3);
    }
    else
    {
        newH = h;
        newW = w;
    }

    top = (screenH - (int) (h * get_aspect(h,screenH))) / 2;
    left = (screenW - (int) (w * get_aspect(h,screenH))) / 2;

    for (; left > 0; left -= newW)
    {}
        for (; top > 0; top -= newH)
        {}

    for (x = left; x < screenW; x += newW) {
        for (y = top; y < screenH; y += newH)
            imlib_blend_image_onto_image (image, 0, 0, 0, w, h, x, y, newW, newH);
        }

 if (modifier != NULL) {
        imlib_context_set_color_modifier(modifier);
        imlib_apply_color_modifier();
        imlib_free_color_modifier();
        modifier = NULL;
      }

    imlib_render_image_on_drawable(0, 0);
    imlib_free_image_and_decache ();
    imlib_free_color_range();
    XKillClient (display, AllTemporary);
    XSetCloseDownMode (display, RetainTemporary);

    XSetWindowBackgroundPixmap (display, RootWindow (display, 0), pixmap);
    XClearWindow (display, RootWindow (display, 0));
    XFlush (display);
    XSync (display, False);
    imlib_context_pop();
    imlib_context_free(context);
}







































double get_aspect(int x, int screenH)
{

return (((double) screenH) / (double) x);

}
int get_adjusted_by_aspect(double aspect, int x)
{
    return (((int) (x * aspect)));
}
