//Copyright 2015 Erik De Rijcke
//
//Licensed under the Apache License,Version2.0(the"License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing,software
//distributed under the License is distributed on an"AS IS"BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package org.westmalle.wayland.bootstrap;

import org.westmalle.wayland.core.LifeCycle;
import org.westmalle.wayland.core.PointerDevice;
import org.westmalle.wayland.protocol.WlKeyboard;
import org.westmalle.wayland.protocol.WlSeat;
import org.westmalle.wayland.x11.X11SeatFactory;

import java.util.logging.Logger;

public class Boot {

    private static final Logger LOGGER   = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    private static final String BACK_END = "BACK_END";


    public static void main(final String[] args) {

        Thread.setDefaultUncaughtExceptionHandler((thread,
                                                   throwable) -> {
            LOGGER.severe("Got uncaught exception " + throwable.getMessage());
            throwable.printStackTrace();
        });

        LOGGER.info("Starting Westmalle");

        initBackEnd();
    }

    private static void initBackEnd() {
        final Boot boot = new Boot();

        String backEnd = System.getProperty(BACK_END);
        if (backEnd == null) {
            backEnd = "";
        }

        switch (backEnd) {
            case "X11Egl":
                boot.strap(DaggerX11EglCompositor.create());
                break;

            case "DispmanxEgl":
                boot.strap(DaggerDispmanxEglCompositor.create());
                break;

            default:
                //TODO if wayland display -> wayland else if X display -> x11 else if nothing -> kms
                boot.strap(DaggerX11EglCompositor.create());
        }
    }

    private void strap(final DispmanxEglCompositor dispmanxEglCompositor) {
        /*
         * Keep this first as weston demo clients *really* like their globals
         * to be initialized in a certain order, else they segfault...
         */
        final LifeCycle lifeCycle = dispmanxEglCompositor.lifeCycle();

        //TODO setup seat
        //dispmanxEglCompositor.seatFactory().create("seat0",...);

        //start the compositor
        lifeCycle.start();
    }

    private void strap(final X11EglCompositor x11EglCompositor) {
        /*
         * Keep this first as weston demo clients *really* like their globals
         * to be initialized in a certain order, else they segfault...
         */
        final LifeCycle lifeCycle = x11EglCompositor.lifeCycle();

        //setup seat for input support
        //get the seat that listens for input on the X connection and passes it on to a wayland seat.
        final WlSeat wlSeat = x11EglCompositor.wlSeat();

        //setup keyboard focus tracking to follow mouse pointer
        final WlKeyboard wlKeyboard = wlSeat.getWlKeyboard();
        final PointerDevice pointerDevice = wlSeat.getWlPointer()
                                                  .getPointerDevice();
        pointerDevice.getPointerFocusSignal()
                     .connect(event -> wlKeyboard.getKeyboardDevice()
                                                 .setFocus(wlKeyboard.getResources(),
                                                           pointerDevice.getFocus()));
        //start the compositor
        lifeCycle.start();
    }
}
@@ -698,16 +698,63 @@
 Device;%0A
+import org.westmalle.wayland.core.TouchDevice;%0A
 import o
@@ -844,57 +844,8 @@
 eat;
-%0Aimport org.westmalle.wayland.x11.X11SeatFactory;
 %0A%0Aim
@@ -2422,36 +2422,30 @@
     
-//TODO setup seat%0A        //
+final WlSeat wlSeat = 
 disp
@@ -2479,30 +2479,1379 @@
 ry()
-.create(%22seat0%22,...);%0A
+%0A                                                   .create(%22seat0%22,%0A                                                           %22%22,%0A                                                           %22pc105%22,%0A                                                           %22us%22,%0A                                                           %22qwerty%22,%0A                                                           %22%22);%0A%0A        //setup keyboard focus tracking to follow mouse pointer & touch%0A        final WlKeyboard wlKeyboard = wlSeat.getWlKeyboard();%0A        final PointerDevice pointerDevice = wlSeat.getWlPointer()%0A                                                  .getPointerDevice();%0A        final TouchDevice touchDevice = wlSeat.getWlTouch()%0A                                              .getTouchDevice();%0A%0A        pointerDevice.getPointerFocusSignal()%0A                     .connect(event -%3E wlKeyboard.getKeyboardDevice()%0A                                                 .setFocus(wlKeyboard.getResources(),%0A                                                           pointerDevice.getFocus()));%0A//        touchDevice.getTouchFocusSignal()%0A//                   .connect(event -%3E wlKeyboard.getKeyboardDevice()%0A//                                               .setFocus(wlKeyboard.getResources(),%0A//                                                         pointerDevice.getFocus()));
 %0A   

create seat for dispmanx back-end