/*
 * @(#)HTTPClient.java  1.0 11/14/01
 *
 * Copyright (C) 2001-2005  Danger, Inc.
 * 3101 Park Blvd, Palo Alto CA 94306
 * All rights reserved.
 */

package com.danger.HTTPClient;

import danger.app.Event;
import danger.net.HTTPConnection;
import danger.ui.*;


public class HTTPClientWindow
    extends ScreenWindow
    implements Resources
{
    public
    HTTPClientWindow()
    {
        // pass
    }
    
    public void
    onDecoded ()
    {
        getDescendantWithID(ID_CHUNK_LABEL).hide();
        getDescendantWithID(ID_CHUNK_DIVIDER).hide();
    }
    
    public final boolean
    eventWidgetUp(int inWhichWidget, Event event)
    {
        switch (inWhichWidget) {
        case Event.DEVICE_BUTTON_CANCEL:
        case Event.DEVICE_BUTTON_BACK:
            getApplication().returnToLauncher();
            return true;
        }
        return super.eventWidgetUp(inWhichWidget, event);
    }
    
    public void
    setFlags (short flags)
    {
        CheckBox returnHeaders = (CheckBox) getDescendantWithID(ID_CHECKBOX_HEADERS);
        CheckBox useHTTPS = (CheckBox) getDescendantWithID(ID_CHECKBOX_HTTPS);
        CheckBox redirect = (CheckBox) getDescendantWithID(ID_CHECKBOX_REDIRECT);
        CheckBox chunk = (CheckBox) getDescendantWithID(ID_CHECKBOX_CHUNK);
        CheckBox useWap = (CheckBox) getDescendantWithID(ID_CHECKBOX_WAP);
        
        returnHeaders.setValue((flags & HTTPConnection.RETURN_HEADERS) != 0 ? 1 : 0);
        useHTTPS.setValue((flags & HTTPConnection.USE_HTTPS) != 0 ? 1 : 0);
        redirect.setValue((flags & HTTPConnection.CLIENT_REDIRECT) != 0 ? 1 : 0);
        chunk.setValue((flags & HTTPConnection.MULTIPART) != 0 ? 1 : 0);
        useWap.setValue((flags & HTTPConnection.USEWAP) != 0 ? 1 : 0);
    }
    
    public short
    getFlags()
    {
        CheckBox returnHeaders = (CheckBox) getDescendantWithID(ID_CHECKBOX_HEADERS);
        CheckBox useHTTPS = (CheckBox) getDescendantWithID(ID_CHECKBOX_HTTPS);
        CheckBox redirect = (CheckBox) getDescendantWithID(ID_CHECKBOX_REDIRECT);
        CheckBox chunk = (CheckBox) getDescendantWithID(ID_CHECKBOX_CHUNK);
        CheckBox useWap = (CheckBox) getDescendantWithID(ID_CHECKBOX_WAP);
        
        short flags = (short)((returnHeaders.getValue() == 1 ? HTTPConnection.RETURN_HEADERS : 0) |
                (useHTTPS.getValue() == 1 ? HTTPConnection.USE_HTTPS : 0) |
                (redirect.getValue() == 1 ? HTTPConnection.CLIENT_REDIRECT : 0) |
                (chunk.getValue() == 1 ? HTTPConnection.MULTIPART : 0) |
                (useWap.getValue() == 1 ? HTTPConnection.USEWAP : 0));
        
        PopupMenu menu = (PopupMenu) getDescendantWithID(ID_METHOD_POPUP);
        switch (menu.getValueAsItemID()) {
        case ID_METHOD_DEFAULT:
            flags |= (METHOD_DEFAULT << FLAG_METHOD_SHIFT);
            break;
        case ID_METHOD_HEAD:
            flags |= (METHOD_HEAD << FLAG_METHOD_SHIFT);
            break;
        case ID_METHOD_GET:
            flags |= (METHOD_GET << FLAG_METHOD_SHIFT);
            break;
        case ID_METHOD_POST:
            flags |= (METHOD_POST << FLAG_METHOD_SHIFT);
            break;
        case ID_METHOD_PUT:
            flags |= (METHOD_PUT << FLAG_METHOD_SHIFT);
            break;
        case ID_METHOD_DELETE:
            flags |= (METHOD_DELETE << FLAG_METHOD_SHIFT);
            break;
        case ID_METHOD_OPTIONS:
            flags |= (METHOD_OPTIONS << FLAG_METHOD_SHIFT);
            break;
        case ID_METHOD_TRACE:
            flags |= (METHOD_TRACE << FLAG_METHOD_SHIFT);
            break;
        }
        
        return flags;
    }
    
    public void
    setBody (String body)
    {
        EditText text = (EditText) getDescendantWithID(ID_BODY);
        text.setText(body);
        text.setInsertionPoint(0);
        text.invalidate();
        text.show();
        ImageView view = (ImageView) getDescendantWithID(ID_IMAGE);
        view.hide();
        invalidate();
    }
    
    public void
    setImage (Bitmap p)
    {
        EditText text = (EditText) getDescendantWithID(ID_BODY);
        text.hide();
        ImageView view = (ImageView) getDescendantWithID(ID_IMAGE);
        view.setImage(p);
        view.setVisible(true);
        view.show();
    }
    
    public void
    setResponseHeaders (String headers)
    {
        EditText text = (EditText) getDescendantWithID(ID_HEADERS);
        text.setText(headers);
        text.setInsertionPoint(0);
        
        View divider = getDescendantWithID(ID_HEADERS_DIVIDER);
        divider.setVisible(true);
        if (headers.length() > 0) {
            text.show();
            divider.show();
        } else {
            text.hide();
            divider.hide();
        }
        
        text.invalidate();
        divider.invalidate();
    }
    
    public void
    setChunk (int sofar, int len)
    {
        StaticText text = (StaticText) getDescendantWithID(ID_CHUNK_LABEL);
        View divider = getDescendantWithID(ID_CHUNK_DIVIDER);
        if (len == 0) {
            // no chunk data
            text.hide();
            divider.hide();
            invalidate();
            return;
        }
        
        int percent = (sofar * 100) / len;
        text.setText("Chunked: received " + percent + "%");
        text.setVisible(true);
        text.invalidate();
        text.show();
        divider.setVisible(true);
        divider.show();
        invalidate();
    }
    
    public String
    getURL ()
    {
        return ((TextField) getDescendantWithID(ID_URL_FIELD)).toString();
    }
    
    public void
    setURL (String url)
    {
        ((TextField) getDescendantWithID(ID_URL_FIELD)).setText(url);
    }
    
    public String
    getHeaders ()
    {
        System.err.println(((EditText) getDescendantWithID(ID_HEADERS_FIELD)).toString());
        return ((EditText) getDescendantWithID(ID_HEADERS_FIELD)).toString();
    }
    
    public void
    setHeaders (String headers)
    {
        ((EditText) getDescendantWithID(ID_HEADERS_FIELD)).setText(headers);
    }
    
    public String
    getPostData ()
    {
        return ((TextField) getDescendantWithID(ID_POSTDATA_FIELD)).toString();
    }
    
    public void
    setPostData (String data)
    {
        ((TextField) getDescendantWithID(ID_POSTDATA_FIELD)).setText(data);
    }
    
    
    
    // request method specification
    // (this should go into libdanger soon)
    public static final int FLAG_METHOD_MASK = 0xf;
    public static final int FLAG_METHOD_SHIFT = 8;
    public static final int METHOD_DEFAULT = 0;
    public static final int METHOD_HEAD = 1;
    public static final int METHOD_GET = 2;
    public static final int METHOD_POST = 3;
    public static final int METHOD_PUT = 4;
    public static final int METHOD_DELETE = 5;
    public static final int METHOD_OPTIONS = 6;
    public static final int METHOD_TRACE = 7;
}
