// Init
UI.moduleBuilder['FTE']['fte_netIpInputPanel'] = function(){
    var initData = {
        ImgBg:"setting/dialog_bg.png",
        InputDialogBg:UI.window_bg,
        numIpt0:"setting/input_password_focused.png",
        numIpt1:"setting/input_password_normal.png",
        numIpt2:"setting/input_password_normal.png",
        numIpt3:"setting/input_password_normal.png",
        Bg:UI.window_bg,
        DeleteBg:UI.item_bg,
        NumDelete:"setting/key_delete.png",
        ZeroBg:UI.item_bg,
        PreBg:UI.item_bg,
        NextBg:UI.item_bg,
        InputDialogTitle: "[SE0175]",//"IP Address",
        PreText:"[GE0011]", // "Previous",
        NextText:"[GE0010]" //"Next"
    };
    var template = Util.readJson("module/FTE/fte_netIpInputPanel.json");
    var NetIpInputPanel = SubModule.create(initData, template);
    var InputDialog = UI.getItem(NetIpInputPanel, "InputDialog");
    var numberInput = UI.getItem(InputDialog, "numberInput");
    var NumPadPanel = UI.getItem(NetIpInputPanel, "NumPadPanel");
    var NumPadList = UI.getItem(NumPadPanel, "NumPadList");

    var info = {
        template : "NumPadTile",
        direction : CONST.GIRD_DIRECTION.H,
        breakNum : 3,
        distanceX : 40,
        distanceY: 20,
        container : NumPadList
    };
    var allArray =[];
    var NumData = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
    for (var i = 0; i < 9; i++) {
        allArray.push(
            {
                tileBg:UI.item_bg,
                Num : NumData[i],
                name : "NumData" + i
            }
        )
    }
    info.container = NumPadList;
    // ����Gird.create�������Gird����ӵ�ê��λ����
    Gird.create(allArray, info);
    // �������ַ�Χ��������ʾ���µ�ͼ��
    //UI.addMask(NumPadList, 988, 354);
    // ������Ұ��Χ�������㳬���˷�Χʱ�����gird�ᷴ���ƶ����ﵽ����Ч��˴����õ�x��y��ȫ����꣩
    //UI.setView(NumPadList, 988, 354);

    NetIpInputPanel.currFocus = UI.getItem(NumPadList, "NumData0");
    var NumData2 = UI.getItem(NumPadList, "NumData2");
    var NumData5 = UI.getItem(NumPadList, "NumData5");
    var NumData6 = UI.getItem(NumPadList, "NumData6");
    var NumData7 = UI.getItem(NumPadList, "NumData7");
    var NumData8 = UI.getItem(NumPadList, "NumData8");
    var NumPadDelete = UI.getItem(NumPadPanel, "NumPadDelete");
    var NumPadZero = UI.getItem(NumPadPanel, "NumPadZero");
    var NumPadPre = UI.getItem(NumPadPanel, "NumPadPre");
    var NumPadNext = UI.getItem(NumPadPanel, "NumPadNext");
    NumData2.rightTo = "NumPadDelete";
    NumData5.rightTo = "NumPadDelete";
    NumData8.rightTo = "NumPadZero";
    NumPadDelete.leftTo = "NumData2";
    //NumPadDelete.upTo = "numIpt1";
    NumPadZero.leftTo = "NumData8";
    //numberInput.downTo = "NumData0";
    //NumPadList.upTo = "numIpt1";
    NumData6.downTo = "NumPadPre";
    NumData7.downTo = "NumPadPre";
    NumData8.downTo = "NumPadNext";
    NumPadZero.downTo = "NumPadNext";
    NumPadPre.upTo = "NumData0";
    NumPadNext.upTo = "NumData0";
    NumPadPre.rightTo = "NumPadNext";
    NumPadNext.leftTo = "NumPadPre";




//    var NetIpInputPanel = UI.getSubModule("fte_netIpInputPanel");
    var netIpSet = UI.getSubModule("fte_netIpSet");
//    var InputDialog = UI.getItem(NetIpInputPanel, "InputDialog");
    var InputDialogTitle = UI.getItem(InputDialog, "InputDialogTitle");
    /*var NumPadPanel = UI.getItem(NetIpInputPanel, "NumPadPanel");
    var NumPadList = UI.getItem(NumPadPanel,"NumPadList");
    var NumPadDelete = UI.getItem(NumPadPanel,"NumPadDelete");
    var NumPadZero = UI.getItem(NumPadPanel,"NumPadZero");
    var NumPadPre = UI.getItem(NumPadPanel, "NumPadPre");
    var NumPadNext = UI.getItem(NumPadPanel, "NumPadNext");*/
    var NextText = UI.getItem(NumPadNext,"NextText");
    // ��������Ϊ���������
    var noIptBar = UI.getItem(NetIpInputPanel, "numberInput");
    var noIpt0 = UI.getItem(noIptBar, "numIpt0");
    var noIpt1 = UI.getItem(noIptBar, "numIpt1");
    var noIpt2 = UI.getItem(noIptBar, "numIpt2");
    var noIpt3 = UI.getItem(noIptBar, "numIpt3");
    var NumInfo = {
        initVal : "",
        min : 0,
        max : 255,
        fontSize : 40,
        align:"center",
        color : "#FFFFFF"
    };

    NumberInput.create(noIpt0, NumInfo);
    NumberInput.create(noIpt1, NumInfo);
    NumberInput.create(noIpt2, NumInfo);
    NumberInput.create(noIpt3, NumInfo);

    var NeInputTitle =[
        {
            "title": "[SE0175]",//"IP Address",
            "index": 0
        },
        {
            "title": "[SE0618]",// "Subnet Mask",
            "index": 1
        },
        {
            "title": "[SE0177]",// "Gateway",
            "index": 2
        },
        {
            "title": "[SE0178]",//"DNS Server 1",
            "index": 3
        },
        {
            "title": "[SE0179]",// "DNS Server 2",
            "index": 4
        }
    ];
    //0 ip 1 mask 2 gateway 3 dns1 4 dns2
    var ipList =[
        [172,16,0,0],
        [173,122,10,8],
        [174,15,18,6],
        [175,16,24,0],
        [176,18,20,1]
    ];

    var ipListArray = [];   //存放输入的数据
    for (var i = 0; i < 16; i++) {
        ipListArray.push("");
    }

    NetIpInputPanel.afterPush = function(){
        initPageData();
        RefreshInputAddressData();
        NetIpInputPanel.currFocus = UI.getItem(NumPadList, "NumData0");
        UI.focusTo(UI.getItem(NumPadList, "NumData0"));
    };
    NetIpInputPanel.onkey_BACKSPACE = function() {
        netIpSet.setIpData(ipListArray);
        UI.popSubModule();
    };
    function  RefreshInputAddressData(){
        if(NetIpInputPanel.curInputflag < 0){
            NetIpInputPanel.curInputflag = 0;
        }
        UI.setText(InputDialogTitle,NeInputTitle[NetIpInputPanel.curInputflag].title);
        noIpt0.children[1].text = ipList[NetIpInputPanel.curInputflag][0]+"";
        noIpt1.children[1].text = ipList[NetIpInputPanel.curInputflag][1]+"";
        noIpt2.children[1].text = ipList[NetIpInputPanel.curInputflag][2]+"";
        noIpt3.children[1].text = ipList[NetIpInputPanel.curInputflag][3]+"";
        UI.updateStage();
    }
    function initPageData(){
//        if(tv){
//
//        }else{
            ipList = netIpSet.getIpData();
            ipListArray = arrayConvert(ipList);
            var curInputflag = Math.floor(netIpSet.pageData.tilesIndex / 4);//0 ip 1 mask 2 gateway 3 dns1 4 dns2
            var curInputIndex = netIpSet.pageData.tilesIndex % 4; //页面打开时input 框位置
//        }
        NetIpInputPanel.curInputflag = curInputflag;
        NetIpInputPanel.currInputIndex = curInputIndex;
        NetIpInputPanel.clearFlag = true;
        UI.setText(NextText,"[GE0010]"); //"Next"
        UI.setImage(noIpt0,"setting/input_password_normal.png");
        UI.setImage(noIpt1,"setting/input_password_normal.png");
        UI.setImage(noIpt2,"setting/input_password_normal.png");
        UI.setImage(noIpt3,"setting/input_password_normal.png");
        UI.setImage(eval("noIpt" + NetIpInputPanel.currInputIndex),"setting/input_password_focused.png");
    }

    function arrayConvert(arr){
        var target = [];
        for(i = 0; i<arr.length; i++){
            target = target.concat(arr[i]);
        }
        return target;
    }

    NumPadList.onkey_ENTER = function() {
        DBG_INFO("NumPadList.onkey_ENTER"+NetIpInputPanel.currFocus.children[1].children[0]);
        DBG_INFO("NumPadList.onkey_ENTER"+ noIpt1.children[1].text);
        var text = NetIpInputPanel.currFocus.children[1].children[0].text;
        setInputTxtAndIndex(text);
        return true;
    };
    NumPadDelete.onkey_ENTER = function(){
        DeleteInputTxtAndIndex();
        return true;
    };
    NumPadZero.onkey_ENTER = function(){
        var text = NetIpInputPanel.currFocus.children[1].children[0].text;
        DBG_INFO("NumPadZero.onkey_ENTER"+ text);
        setInputTxtAndIndex(text);
        return true;
    };
    NumPadNext.onkey_ENTER = function(){
        DBG_INFO("NumPadNext.onkey_ENTER");
        switch (NetIpInputPanel.currInputIndex){
            case 0:
                setCurrIndexAndImg(0,1);
                break;
            case 1:
                setCurrIndexAndImg(1,2);
                break;
            case 2:
                setCurrIndexAndImg(2,3);
                break;
            case 3:
                if(NetIpInputPanel.curInputflag < 3){
                    setCurrIndexAndImg(3,0);
                    NetIpInputPanel.curInputflag++;
                    RefreshInputAddressData();
                }else{
                    netIpSet.setIpData(ipListArray);
                    UI.popSubModule();
                }
                break;
            default :
                break;
        }
        return true;
    };
    NumPadPre.onkey_ENTER = function(){
        DBG_INFO("NumPadPre.onkey_ENTER");
        switch (NetIpInputPanel.currInputIndex){
            case 0:
                NetIpInputPanel.curInputflag--;
                RefreshInputAddressData();
                break;
            case 1:
                setCurrIndexAndImg(1,0);
                break;
            case 2:
                setCurrIndexAndImg(2,1);
                break;
            case 3:
                setCurrIndexAndImg(3,2);
                break;
            default :
                break;
        }
        return true;
    };

    function setInputTxtAndIndex(keyValue){
        if(NetIpInputPanel.currInputIndex == 0){
            setTextOnChange(0,keyValue);
        }else if(NetIpInputPanel.currInputIndex == 1){
            setTextOnChange(1,keyValue);
        }else if(NetIpInputPanel.currInputIndex == 2){
            setTextOnChange(2,keyValue);
        }else if(NetIpInputPanel.currInputIndex == 3){
            setInput4TextOnChange(3,keyValue);
        }
    }
    function setTextOnChange(index,text){
        var target = UI.getItem(noIptBar, "numIpt"+index);
        var targetNext = UI.getItem(noIptBar, "numIpt"+(index+1));
        var txt = target.children[1];
        var txtNext = targetNext.children[1];
        var tempTxt = txt.text;
        if(NetIpInputPanel.clearFlag){
            txt.text = text+"";
            ipListArray[NetIpInputPanel.curInputflag * 4 + index] = txt.text;
            NetIpInputPanel.clearFlag = false;
        }else{
            tempTxt = tempTxt + text;
            if (tempTxt.length <= txt.max.length && parseInt(tempTxt) <= parseInt(txt.max) && parseInt(tempTxt) >= parseInt(txt.min)) {
                txt.text = tempTxt;
                ipListArray[NetIpInputPanel.curInputflag * 4 + index] = txt.text;
            }else{
                //NetIpInputPanel.clearFlag = true;
                txtNext.text = text+"";
                ipListArray[NetIpInputPanel.curInputflag * 4 + index + 1] = txtNext.text;
                setCurrIndexAndImg(index,index+1);
            }
        }
        UI.updateStage();
    }
    function setInput4TextOnChange(index,text){
        var target = UI.getItem(noIptBar, "numIpt"+index);
        var targetNext = UI.getItem(noIptBar, "numIpt1");
        var txt = target.children[1];
        var txtNext = targetNext.children[1];
        var tempTxt = txt.text;
        if(NetIpInputPanel.clearFlag){
            txt.text = text+"";
            ipListArray[NetIpInputPanel.curInputflag * 4 + index] = txt.text;
            NetIpInputPanel.clearFlag = false;
        }else{
            tempTxt = tempTxt + text;
            if (tempTxt.length <= txt.max.length && parseInt(tempTxt) <= parseInt(txt.max) && parseInt(tempTxt) >= parseInt(txt.min)) {
                txt.text = tempTxt;
                ipListArray[NetIpInputPanel.curInputflag * 4 + index] = txt.text;
                if((NetIpInputPanel.curInputflag == 2) && (txt.text == txt.max.length)){
                    UI.setText(NextText,"[GE0007]");//"OK",
                }
            }else{
                if(NetIpInputPanel.curInputflag == 3){
                    UI.setText(NextText,"[GE0007]");//"OK",
                    return;
                }
                NetIpInputPanel.clearFlag = true;
                NetIpInputPanel.curInputflag ++ ;
                setCurrIndexAndImg(3,0);
                RefreshInputAddressData();
            }
        }
        UI.updateStage();
    }

    function setCurrIndexAndImg(curr,next){
        var Currtarget = UI.getItem(noIptBar, "numIpt"+curr );
        var Nexttarget = UI.getItem(noIptBar, "numIpt"+next);
        UI.setImage(Currtarget,"setting/input_password_normal.png");
        UI.setImage(Nexttarget,"setting/input_password_focused.png");
        NetIpInputPanel.currInputIndex = next;
    }
    function DeleteInputTxtAndIndex(){
        if(NetIpInputPanel.currInputIndex == 0){
            noIpt0.children[1].text = noIpt0.children[1].text.slice(0,-1);
            UI.updateStage();
        }else if(NetIpInputPanel.currInputIndex == 1){
            if( noIpt1.children[1].text != ""){
                noIpt1.children[1].text = noIpt1.children[1].text.slice(0,-1);
                UI.updateStage();
            }else{
                noIpt0.children[1].text =noIpt0.children[1].text.slice(0,-1);
                UI.updateStage();
                setCurrIndexAndImg(1,0);
            }
        }else if(NetIpInputPanel.currInputIndex == 2){
            if(noIpt2.children[1].text != ""){
                noIpt2.children[1].text = noIpt2.children[1].text.slice(0,-1);
                UI.updateStage();
            }else{
                noIpt1.children[1].text = noIpt1.children[1].text.slice(0,-1);
                UI.updateStage();
                setCurrIndexAndImg(2,1);
            }
        }else if(NetIpInputPanel.currInputIndex == 3){
            if(noIpt3.children[1].text != ""){
                noIpt3.children[1].text = noIpt3.children[1].text.slice(0,-1);
                UI.updateStage();
            }else{
                noIpt2.children[1].text = noIpt2.children[1].text.slice(0,-1);
                UI.updateStage();
                setCurrIndexAndImg(3,2);
            }
        }
    }

    var proIpNumKey = function() {
        var keyValue = CONST.NUMIPT_VALUE[UI.currKeyValue];
        setInputTxtAndIndex(keyValue);
    };
    NumPadPanel.onkey_ZERO = proIpNumKey;
    NumPadPanel.onkey_ONE = proIpNumKey;
    NumPadPanel.onkey_TWO = proIpNumKey;
    NumPadPanel.onkey_THREE = proIpNumKey;
    NumPadPanel.onkey_FOUR = proIpNumKey;
    NumPadPanel.onkey_FIVE = proIpNumKey;
    NumPadPanel.onkey_SIX = proIpNumKey;
    NumPadPanel.onkey_SEVEN = proIpNumKey;
    NumPadPanel.onkey_EIGHT = proIpNumKey;
    NumPadPanel.onkey_NINE = proIpNumKey;
};