package com.oberon.client;
/*
 * Copyright 2008-2014 Mirko Solazzi - OBEROn Platform [www.oberonplatform.com]
 *
 * Licensed under the Apache License, Version 2.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.
 * 
 */

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import com.oberon.ooql.connection.ConnectionManager;

public class CustomClient extends OberonClient {

	public CustomClient(String application_MainMenu) {
		super(application_MainMenu);
		setApp(this);
	}

	// Request Handlers
	public boolean handleRequest(ApplicationRequest request) {
		if (!super.handleRequest(request)) {
			String page = request.getRequestPage();
			if (page.endsWith("mypage.jsp")) {
				doMyPage(request);
			}
			// Add the custom page handlers
		}
		return true;
	}

	public void doHome(ApplicationRequest request) {
		try {
			String sTitle = Application.getCommonTranslation("Welcome");
			Composite globalpanel = createPagePanel(sTitle, "", false, request);
			if (globalpanel != null) {
				Composite panel = new Composite(globalpanel, SWT.NONE);
				panel.setLayoutData(new GridData(GridData.FILL_BOTH));

				GridLayout imageLayout = new GridLayout();
				imageLayout.horizontalSpacing = 1;
				panel.setLayout(imageLayout);
				Label bImage = new Label(panel, SWT.CENTER);
				Image homeImg = HTMLUtil.getImage(Application.getApp().application_MainMenu + "_home.jpg", 0, 400, session);
				if (homeImg != null && homeImg.getImageData().width > 1) {
					bImage.setImage(homeImg);
				} else {
					try {
						bImage.setImage(Application.getIcon("home.jpg"));
					} catch (Exception ex) {
					}
				}
				bImage.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));

				Text aboutText = new Text(panel, SWT.BORDER | SWT.MULTI | SWT.WRAP);
				aboutText.setFont(Application.globalfont);
				GridData txGD = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
				txGD.minimumHeight = 45;
				aboutText.setLayoutData(txGD);
				String sText = name + "\n";
				sText += "OBEROn Platform [version: " + ConnectionManager.version + " - build " + ConnectionManager.build + "]   ";
				sText += ConnectionManager.copyright;
				aboutText.setEditable(false);
				aboutText.setText(sText);

				addPageToContainer(globalpanel, sTitle, request);
			}
		} catch (Exception ex) {
			log(ex.getMessage());
		}
	}

	private void doMyPage(ApplicationRequest request) {
		try {
			//ApplicationSession session = (ApplicationSession) request.getSession();
			String sTitle = "My Page Title";
			String sSubTitle = "My Page SubTitle";
			Composite globalpanel = createPagePanel(sTitle, sSubTitle, false, request);
			if (globalpanel != null) {
				Composite panel = new Composite(globalpanel, SWT.NONE);
				panel.setLayoutData(new GridData(GridData.FILL_BOTH));

				// Add the Panel Content

				addPageToContainer(globalpanel, sTitle, request);
			}
		} catch (Exception ex) {
			log(ex.getMessage());
		}
	}

	// CSS Styles
	public void setCSSStyles() {
		super.setCSSStyles();

		// Modify default styles
		CSSStyle titlepanel = HTMLUtil.getCSSStyle(TAGS.CSS_TitlePanel);
		titlepanel.setBackGroundImage("menu_files/topbg.png");

		CSSStyle subtitle = HTMLUtil.getCSSStyle(TAGS.CSS_SubTitle);
		subtitle.setColor("#fdb435");

		// Add custom styles
		CSSStyle mystyle = new CSSStyle("mystyle");
		mystyle.setFont("Arial", 14, SWT.BOLD);
		mystyle.setColor("#0033FA");
		mystyle.setBackGroundColor(150, 150, 150);
		mystyle.setTextAlignment("right");
		HTMLUtil.addCSSStyle(mystyle);

	}

	public static void main(String[] args) throws Exception {

		// Set here the application menu if the user cannot set it inside the config
		// (.ini) file
		String applicationMenu = null;
		CustomClient client = new CustomClient(applicationMenu);

		while (!client.shell.isDisposed()) {
			if (!getApp().display.readAndDispatch())
				getApp().display.sleep();
		}

	}

}