import java.io.IOException;
import java.io.Writer;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordStoreException;

/**
 * 对话框,替换原SimpleForm的主要工作
 * @author xuh
 *
 */
public class DialogBox {
 
  public Menu leftMenu = null; //左按钮
 public Menu rightMenu = null; //右按钮
 
 private int dialogState = -1;
 
 private Object obj = null;
 
 public String name = null; //对话框的名字
 
 public String[] text = null; //对话框内显示的内容
 
 private ControlPep controller = null;
 
 private UICanvasBase canvas = null;
 
 public Menu leftMenuLast = null; //保存跳出对话框前的左按钮
 
 public Menu rightMenuLast = null; //保存跳出对话框前的右按钮
 
 private int windowsHeight = (UICanvasBase.smallFontHeight + 2) * 6;  //对话框的高度 (这是含标题的高度)
 String)obj).lastIndexOf('/')+1, ((String)obj).length());
    text = Tools.cutString(font, tmpStrs, textWid);
    yesOrNoCommand();
    break;
   case CONFIRM_RegInfo:
    text = Tools.cutString(font, "恭喜注册成功!您的贝多号是" + controller.localEvent.rmsSystem.db3_userjid + "初始密码是" + controller.localEvent.rmsSystem.db4_password + "请妥善保存账号信息。点击确认将自动登录.", textWid);
    leftMenu = new Menu("确认",null, null);
    rightMenu = new Menu("  ", null, null);
    break;
   case CONFIRM_UnAuth:
    text = Tools.cutString(font, "此贝多号还没有绑定手机,无法登录。请到wap.bedo.cn绑定手机;或其它贝多号登录后到『社区-安全中心-绑定』。", textWid);
    leftMenu = new Menu("确认",null, null);
    rightMenu = new Menu("  ", null, null);
    break;
   case CONFIRM_RegErr:
    String tmpDes = INFO.EMPTY;
    if(controller.regErrorCode.equals("603") && 
           tmpDes = controller.regErrorDes;
    }
    text = Tools.cutString(font, tmpDes, textWid);
    leftMenu = new Menu("确认",null, null);
    rightMenu = new Menu("  ", null, null);
    break;
   case ASK_RegErr_SendOrWait:
    text = Tools.cutString(font, controller.regErrorDes + "可能由于网络延迟或其他原因注册未成功,请选择重发注册或继续等待。", textWid);
    leftMenu = new Menu("重发注册", null, null);
    rightMenu = new Menu("继续等待",null,null);
    break;
   case ASK_SendAuth:
    text = Tools.cutString(font, INFO.CONTENT_ASK_sendAuth, textWid);
    leftMenu = new Menu("重发",null, null);
    rightMenu = new Menu("继续等",null,null);
    break;
   case ASK_networkContiOrExit:
    text = Tools.cutString(font, INFO.CONTENT_ASK_networkContOrSett, textWid);
    leftMenu = new Menu("退出&设置", null, null); // -> 0
    rightMenu = new Menu("继续使用",null,null); // -> 1
    break;
   case ASK_fsContiOrExit:
    text = Tools.cutString(font, INFO.CONTENT_ASK_fsContOrSett, textWid);
    leftMenu = new Menu("退出&设置", null, null); // -> 0
    rightMenu = new Menu("继续使用",null,null); // -> 1
    break;
   case ASK_msgListVoiceAlert:
    text = Tools.cutString(font,INFO.CONTENT_ASK_msgListVoiceAlert, textWid);
    leftMenu = new Menu("返回",null,null);
    rightMenu = new Menu(" ",null,null);
    break;
  }
  
  menuWriteOver();
 }
 
 private void yesOrNoCommand(){
  leftMenu = new Menu(INFO.shortString[35],null, null);
  rightMenu = new Menu(INFO.shortString[36],null, null);
 }
 
 //替换掉当前的菜单(对话框显示前的按钮需要保存)
 public void menuWriteOver(){
 
//  leftMenuLast = canvas.leftMenu;
//  rightMenuLast = canvas.rightMenu;
  canvas.setLeftMenu(leftMenu);
  canvas.setRightMenu(rightMenu);
//  canvas.leftMenu = leftMenu;
//  canvas.rightMenu = rightMenu;
  canvas.menuState = UICanvasBase.MS_NOT_OPEN; //跳转到对话框,所有菜单关闭
  
  canvas.dialogIf = true;
 }
 
 //恢复以前的菜单按钮
 public void menuWriteBack(){
  canvas.setLeftMenu(null);
  canvas.setRightMenu(null);
  
  canvas.dialogIf = false;
 }
 
 public void draw(Graphics g){
  int pX = 0;
     int pY = 0;
     int screenW = canvas.screenW;
     int screenH = canvas.screenH;
     int tmpEndY = screenH - UICanvasBase.belowHeight;
     while(pY < canvas.screenH){ //填充半透明背景
      g.drawRGB(DialogBox.rgbs, 0, DialogBox.pixelsSide, pX, pY,
         DialogBox.pixelsSide, DialogBox.pixelsSide, true);
      pX += DialogBox.pixelsSide;
      if(pX > canvas.screenW){
       pY += DialogBox.pixelsSide;
       pX = 0;
      }
     }
     
     if(name == null){
         g.setClip(0, tmpEndY - windowsHeight, screenW, windowsHeight);
      g.setColor(Skin.COLOR_selectedAround);
          g.fillRect(0, tmpEndY - windowsHeight, screenW, windowsHeight);
          g.setColor(130,230,230);
          g.fillRect(1, tmpEndY - windowsHeight + 1, screenW-2, windowsHeight - 2);
         
          g.setColor(Skin.COLOR_bigSelect_Menu);
          g.fillRect(2, tmpEndY - windowsHeight + 2, screenW-4, windowsHeight - 4);
         
         
          g.setColor(0x0);
          g.setClip(0, tmpEndY - contentHeight, screenW, contentHeight);
          for(int i=0;i<text.length;i++){
           g.drawString(text[i], 4, tmpEndY - windowsHeight + 4 + i*(UICanvasBase.smallFontHeight+2), 0);
          }
     }else{
      g.setClip(0, tmpEndY - windowsHeight, screenW, windowsHeight);
      g.setColor(Skin.COLOR_selectedAround);
          g.fillRect(0, tmpEndY - windowsHeight, screenW, windowsHeight);
          g.setColor(Skin.COLOR_smallSelect_Menu);
          g.fillRect(1, tmpEndY - windowsHeight + 1, screenW-2, windowsHeight - 2);
          g.setColor(Skin.COLOR_bigSelect_Menu);
          g.fillRect(2, tmpEndY - windowsHeight + titleHeight + 2, screenW-4, windowsHeight - titleHeight - 4);
          g.setColor(0x0);
          g.drawString(name, 4, tmpEndY - windowsHeight + 2, 0);
          g.setClip(0, tmpEndY - contentHeight, screenW, contentHeight);
          for(int i=0;i<text.length;i++){
           g.drawString(text[i], 4, tmpEndY - windowsHeight + titleHeight + 4 + (i-startLine)*(UICanvasBase.smallFontHeight+2), 0);
          }
     }
 }
 
 public void fireNoMenu(int keyCode){
  switch(keyCode){
   case UICanvasBedo.UP:
    if(startLine > 0)
     startLine--;
    break;
   case Canvas.DOWN:
    if(startLine + contentLineNum < text.length)
     startLine++;
    break;
  }
 }
 
 int tmpPointY = 0;
 public void touchPressed(int x, int y){
  tmpPointY = y;
 }
 
 public void touchReleased(int x, int y){
  if((tmpPointY - y) > (UICanvasBase.smallFontHeight + 2)){//下滚
   fireNoMenu(Canvas.DOWN);
  }else if((y - tmpPointY) > (UICanvasBase.smallFontHeight + 2)){
   fireNoMenu(UICanvasBedo.UP);
  }
  tmpPointY = 0;
 }
 
 public void pressLeftMenu(){
  switch(dialogState){
  case ESSAY_Statement: //免责声明
   break;
  case ASK_RegIf:  //是否注册
//         controller.localEvent.rmsSystem.systemRms = INFO.EMPTY;
//         controller.localEvent.rmsSystem.messageRms = INFO.EMPTY;
         ControlPep.skin.setSkin(0);
         controller.bedoCanvas.setUIState(UICanvasBedo.UI_Logining);
         controller.asyncEvent(INFO.ID_register);
   break;
  case ESSAY_OutHelp:  //外部帮助
   break;
  case ASK_DelAllIf:  //是否肯删除原来的MESSAGE来换号登录
   break;
  case REG_DelAllIf:  //是否肯删除原来的MESSAGE来注册新号
   break;
  case ESSAY_InHelp:  //里面的帮助
   break;
  case ASK_ExitIf:  //是否退出
   controller.asyncEvent(INFO.ID_Exit);
   break;
  case ASK_DelUserMoreIf:  //删除好友吗?
   controller.asyncEvent(INFO.ID_removeUser, new Object[] { ((User) obj).getJid() });
   ((User) obj).getGroup().removeUser(((User) obj).getJid(), true);
   
   if (((User) obj).getGroup().allNum == 0) {
    String groupName = ((User) obj).getGroup().getName();
    ((User) obj).getGroup().getLeadGroup().removeGroup( groupName);
   }
   
   if (((User) obj).beSelected) {
    if (controller.bedoCanvas.buddyListUI == null)
     controller.bedoCanvas.init_buddyList();
    controller.bedoCanvas.buddyListUI.lowerSelect();
   }
   
   controller.bedoCanvas.setUIState(UICanvasBedo.UI_BuddyList);
   break;
  case ASK_DelGroupIf: //删除组吗?
   break;
  case ASK_DelMessageIf:  //是否删除当前消息
   try {
             controller.localEvent.delOneMessage(Integer.parseInt((String)obj));
             if(!controller.bedoCanvas.messageUI.getNowSelectMessage().picUrl.equals(INFO.EMPTY)){//附件
              FileSystem.msgIndexArray = controller.localEvent.removeFile(FileSystem.pathMsgCache
                        ,FileSystem.msgIndexArray
               ,controller.bedoCanvas.messageUI.urlToFileName(controller.bedoCanvas.messageUI.getNowSelectMessage().picUrl));
              //#debug #debug
              System.out.println(controller.bedoCanvas.messageUI.urlToFileName(controller.bedoCanvas.messageUI.getNowSelectMessage().picUrl) + "已删除!");
             }else if(!controller.bedoCanvas.messageUI.getNowSelectMessage().soundUrl.equals(INFO.EMPTY)){//语音
              controller.localEvent.removeFile(FileSystem.pathMsgCache
                                       ,FileSystem.msgIndexArray
                                       ,controller.bedoCanvas.messageUI.urlToFileName(controller.bedoCanvas.messageUI.getNowSelectMessage().soundUrl));
             }                    
            } catch (RecordStoreException e) {
            } catch (IOException e) {
            }
            if(controller.bedoCanvas.messageUI == null)controller.bedoCanvas.init_message();
//            controller.bedoCanvas.messageUI.reSetNums();
            controller.bedoCanvas.messageUI.loadMessage();
            controller.bedoCanvas.messageUI.deleteCurMessage(); //让当前总是选中最后一条消息
   break;
  case ASK_DelAllMessageIf_ButLock:  //是否删除所有消息(锁的除外)
   try {
                controller.localEvent.delAllMessage();
            } catch (RecordStoreException e) {
            } catch (IOException e) {
            }
            if(controller.bedoCanvas.messageUI == null)controller.bedoCanvas.init_message();
            controller.bedoCanvas.messageUI.reSetNums();
            controller.bedoCanvas.messageUI.loadMessage();
   break;
  case ASK_DelAllMessageIf:  //是否清空消息
   try {
                controller.localEvent.clearMessage();
            } catch (RecordStoreException e) {
            }
            if(controller.bedoCanvas.messageUI == null)controller.bedoCanvas.init_message();
            controller.bedoCanvas.messageUI.reSetNums();
            controller.bedoCanvas.messageUI.loadMessage();
   break;
  case ASK_LogoutMSN:  //登出MSN?
   controller.msnState = 6;
            controller.addToAlert(INFO.CONTENT_BELOW_strings[10]);
            if(controller.bedoCanvas.buddyListUI == null)controller.bedoCanvas.init_buddyList();
            UIBuddyList buddyListUI = controller.bedoCanvas.buddyListUI;
            Group groupAll = buddyListUI.groupAll;
//            ((Group)groupAll.underGroup.elementAt(buddyListUI.MSNLocation)).setEmpty();

   break; 
   //#endif
  case CONFIRM_RegInfo:
   controller.regInfoConfirm = true;
   break;
  case CONFIRM_UnAuth:
   controller.bedoCanvas.setUIState(UICanvasBedo.UI_Login);
   controller.bedoCanvas.setFullScreenMode(true);
   controller.unAuthConfirm = 0;
   controller.isCancel = true;
   break;
  case CONFIRM_RegErr:
   controller.regErrConfirm = true;
   controller.bedoCanvas.setUIState(UICanvasBedo.UI_Login);
   break;
  case ASK_RegErr_SendOrWait:
   //重发注册
   controller.regWaitOrSend = 0;
   break;
  case ASK_SendAuth:
   controller.unAuthConfirm = 0;
   break;
  case ASK_networkContiOrExit:
  case ASK_fsContiOrExit:
   controller.continueOrExit = 0;
   break;
  }
  
  menuWriteBack();
 }
 
 
 public void pre***ightMenu(){
  switch(dialogState){
   case CONFIRM_UnAuth:
   case CONFIRM_RegInfo:
   case CONFIRM_RegErr:
   case ASK_msgListVoiceAlert:
    return;
   case ASK_RegErr_SendOrWait:
    //继续等待
    controller.regWaitOrSend = 1;
    break;
   case ASK_SendAuth:
    controller.unAuthConfirm = 1;
    break;
   case ASK_networkContiOrExit:
   case ASK_fsContiOrExit:
    controller.continueOrExit = 1;
    break;
  }
  menuWriteBack();
 }
 
 /*private void sample(){
  switch(dialogState){
   case ESSAY_Statement: //免责声明
    break;
   case ASK_RegIf:  //免责声明后的是否注册
    break;
   case ESSAY_OutHelp:  //外部帮助
    break;
   case ASK_DelAllIf:  //是否肯删除原来的MESSAGE来换号登录
    break;
   case REG_DelAllIf:  //是否肯删除原来的MESSAGE来注册新号
    break;
   case ESSAY_InHelp:  //里面的帮助
    break;
   case ASK_ExitIf:  //是否退出
    break;
   case ASK_DelUserMoreIf:  //删除好友吗?
    break;
   case ASK_DelGroupIf: //删除组吗?
    break;
   case ASK_DelMessageIf:  //是否删除当前消息
    break;
   case ASK_DelAllMessageIf_ButLock:  //是否删除所有消息(锁的除外)
    break;
   case ASK_DelAllMessageIf:  //是否清空消息
    break;
   case ASK_LogoutMSN:  //登出MSN?
    break;
   case ASK_LogoutYAHOO: //登出YAHOO?
    break;
   case ESSAY_WaringAboutUpdateMessage: //强制更新
    break;
   case ASK_BlockUserIf: //确认禁止该人进入聊天室?
    break;
   case ASK_KickOffUserIf:  //确认踢人?
    break;
   case ASK_UnBlockUserIf:  //确认解禁某人进入聊天室?
    break;
   case ASK_BlackUserIf:  //确认加入黑名单?
    break;
   case ASK_DelUserIf:  //确认彻底删除好友?
    break;
   case ASK_UnBlackUserIf:  //确认撤消黑名单?
    break;
   case ASK_UnBlackAndAddUserIf:  //黑名单出来的人加为好友么?
    break;
   case ASK_RegMessageIf:  //确认发送注册短信
    break;
   case ASK_AuthenticateIf: //确认发送验证短信
    break;
   case Authenticateing:  //验证中
    break;
   case AboutBedo:  //Bedo关于
    break;
   case OutAboutBedo:  //外部Bedo关于
    break;
   case ASK_BroadCastToAllIf:  //确认给地图上所有人发送短信
    break;
   case ASK_FindPSWIf:  //确认查找账号密码
    break;
   case ASK_UpdatePicIf: //上传头像图片提示
    break; 
  }
 }*/
 
}