
var CACHE=true;
var SHOW_CACHE_INFO=false;
var NO_SERVER=false;

if(!isString){
var isString=function (obj){
return (typeof (obj)=="string"||obj instanceof String);};}
if(!isNumber){
var isNumber=function (obj){
return (typeof (obj)=="number"&&!isNaN(obj));};}
var FLIST_HOST="";
try{
MyHostNameS;FLIST_HOST=MyHostNameS.replace("https://","https://mhcid-user:Fr1end.Mana9er@");
console.log("---------- MyHostNameS defined (running on ajaxceplay ?)");}catch(e){
console.log("---------- no MyHostNameS (running on surfer ?)");
FLIST_HOST=getHostName().replace("https://","https://mhcid-user:Fr1end.Mana9er@")+"/";}
function enc(str){
return encodeURIComponent(str);}

var stub=[{id:"USERKEY_FOR_XXX",
nickname:"USER -- FOR_XXX",
face:"file:///usr/local/apps_nv/apps/0387878700000010/icon.png"
},
{id:"USERKEY001",
nickname:"USER -- 001",
face:"file:///usr/local/apps_nv/apps/0387878700000018/icon.png"
},
{id:"USERKEY002",
nickname:"USER -- 002",
face:"file:///usr/local/apps_nv/apps/0387878700000029/icon.png"
},
];
function FriendList(auth_token){
if(isString(auth_token)==false)
throw new Error("[FriendList@constructor] Illegal argument : "+auth_token);
this.auth_token=auth_token;}
FriendList.prototype={};
FriendList.prototype.FLIST_TYPE_FRIEND="0";
FriendList.prototype.FLIST_TYPE_REQUEST="1";
FriendList.prototype.FLIST_TYPE_APPROVE="2";
FriendList.prototype.auth_token=null;FriendList.prototype.req_id=-1;
FriendList.prototype.$cache=[];
FriendList.prototype.getMyFriends=function(callback){
var ret=null;
var self=this;
return http_request({
method:"GET",
url:FLIST_HOST+"mhcid/friend-list?token="+enc(self.auth_token),
sync:false,
onload:function(status,head,body){
if(status==200){
try{
if(NO_SERVER){
ret=stub;}else{
ret=eval("("+body+")").entries;}
if(CACHE){
self.$updateCache(ret);}
callback(ret);}catch(e){
console.log("Get FriendList unsuccessful : "+e.toSource());
console.log("[body]\n"+body)
callback([]);}
}else {
callback([]);}
}
});};
FriendList.prototype.getUserData=function(usrkey_list,callback){
if(CACHE){
var cdata=this.$retrieveFromCache(usrkey_list);
if(cdata){
if(SHOW_CACHE_INFO){
console.log("FriendList.getUserData() : Cache Hit !");}
append_timer({},30,
function(obj){
delete_timer(obj);
callback(cdata);});
return -1;}
}
var ret=null;
var self=this;
return http_request({
sync:false,
method:"POST",
header:[["content-type","application/json"]],
body:"["+usrkey_list.map(function(v){return "\""+v+"\"";}).toString()+"]",
url:FLIST_HOST
+"mhcid/friend-info?&token="+enc(self.auth_token),
onload:function(status,header,body){
if(status==200){
try{
if(NO_SERVER){
if(usrkey_list.length<=stub.length){
ret=stub.slice(0,usrkey_list.length);}else{
var tmp=stub.slice(0);
for(var i=0;i<usrkey_list.length-stub.length;i++){
tmp.push(stub[i%stub.length]);}
ret=tmp;}
}else{
ret=eval("("+body+")").entries;}
}catch(e){
console.log("Error @ pkg_friends.FriendList.getUserData() : "+e.toSource());
ret=[];}
}else{
ret=[{id:null,nickname:"Cannot Load.",face:""}];}
if(CACHE){
self.$updateCache(ret);}
callback(ret);},
});};
FriendList.prototype.$retrieveFromCache=function(usrkey_list){
var ret=this.$cache.filter(
function(val,idx,arry){
return usrkey_list.some(function(v,i,a){return (v==val.id);});}
);
if(ret.length==usrkey_list.length){
return ret;}else{
return null;}
};
FriendList.prototype.$updateCache=function(fdata_list){
if(!fdata_list||fdata_list.length==0)return;
if(SHOW_CACHE_INFO){
console.log("$updateCache : (before) length="+this.$cache.length);}
fdata_list.map(
function(val,idx,arry){
var m=this.$cache.filter(function(v,i,a){return (val.id==v.id)});
if(m.length==0){this.$cache.push(val);}else{for(var x in m[0]){
m[0][x]=val[x];}
}
},
this);
if(SHOW_CACHE_INFO){
console.log("             : (after)  length="+this.$cache.length);
for(var i=0;i<this.$cache.length;i++){
console.log("   "+i+" = "+this.$cache[i].id+"\t"+this.$cache[i].nickname);}
console.log("______________\n");}
};
FriendList.prototype.getFriendData=function(usrkey_list){
console.log("/**************************************************/");
console.log("  WARNING - FriendList.prototype.getFriendData()");
console.log("            This API (function) will be deprecated.");
console.log("            Please use \"getUserData()\".");
console.log("/**************************************************/");
var ret=null;
if(NO_SERVER){
if(usrkey_list.length<=stub.length){
return stub.slice(0,usrkey_list.length);}else{
var tmp=stub.slice(0);
for(var i=0;i<usrkey_list.length-stub.length;i++){
tmp.push(stub[i%stub.length]);}
return tmp;}
}
cancel_http_request(this.req_id);
this.req_id=http_request({
sync:true,method:"POST",
header:[["content-type","application/json"]],
body:"["+usrkey_list.map(function(v){return "\""+v+"\"";}).toString()+"]",
url:FLIST_HOST
+"mhcid/friend-info?&token="+enc(this.auth_token),
onload:function(status,header,body){
if(status==200){
ret=eval("("+body+")").entries;}else{
ret=[{id:null,nickname:"Cannot Load.",face:""}];}
},
});
return ret;};
FriendList.prototype.getGUI=function(focus_moveto,action_func,finish_func){
var gui=new FriendListGUI(focus_moveto,action_func);

this.getMyFriends(function(data_list){
if(data_list==null){
gui.setDataList([{id:null,nickname:"ERROR.",face:""}]);}else if(data_list.length==0){
gui.setDataList([{id:null,nickname:"No DATA.",face:""}]);}else{
gui.setDataList(data_list);}
if(finish_func){
finish_func(data_list);}
});
return gui;};
FriendList.prototype.retrieveFriends=function(type,auth_token){
console.log("/**************************************************/");
console.log("  WARNING - FriendList.prototype.retrieveFriends()");
console.log("            This API (function) will be deprecated.");
console.log("/**************************************************/");
var ret=null;
if(NO_SERVER)ret=stub;
if(!auth_token){
auth_token=this.auth_token;}
cancel_http_request(this.req_id);
this.req_id=http_request({
sync:true,url:FLIST_HOST
+"mhcid/friend-list?type="+enc(type)+"&token="+enc(auth_token),
onload:function(status,header,body){
if(status==200){
ret=eval("("+body+")").entries;}else{
ret=[{id:null,nickname:"Cannot Load.",face:""}];}
},
});
return ret;};


function SimpleItem(){
this.superClass=container;
this.superClass({});
this.text=new gtextbox({width:this.getWidth(),
height:this.MIN_HEIGHT,
valign:MIDDLE});
this.components=[this.text];}
SimpleItem.prototype=new container({});
SimpleItem.prototype.MIN_HEIGHT=78;
SimpleItem.prototype.text=null;
SimpleItem.prototype.toString=function(){
return this.text.text;};
SimpleItem.prototype.getWidth=function(){
return 240;};
SimpleItem.prototype.getHeight=function(){
return this.text.height;};
SimpleItem.prototype.getTopMargin=function(){
return 0;};
SimpleItem.prototype.setData=function(data){
var dim=get_dimension_textbox({width:this.getWidth(),text:data.toString()});
this.text.height=Math.max(this.MIN_HEIGHT,dim[1]);
setf_text(this.text,data.toString());};
SimpleItem.prototype.setTranslate=function(x,y,z){
if(isNumber(x))this.translate[0]=x;
if(isNumber(y))this.translate[1]=y;
if(isNumber(z))this.translate[2]=z;};
SimpleItem.prototype.action=function(vlist){
console.log("action! (index="+vlist.getSelectedIndex()+") : "+"\""+this.toString()+"\"");
return true;};
SimpleItem.prototype.onFocus=function(b){
return b;};
function VList(focus_moveto,item_class){
this.superClass=container;
this.superClass({});

this.items=[];
this.mask_top=new gbox({width:240,height:100,
visible_p:false,
translate:[0,0,1],
depth_test:true,
color:[0,0,0,0]});
this.mask_bottom=new gbox({width:240,height:100,
visible_p:false,
translate:[0,0,1],
depth_test:true,
color:[0,0,0,0]}),
this.focus_moveto=focus_moveto;
if(item_class){
this.item_class=item_class;}
}
VList.prototype=new container({});
VList.prototype.cursor=0;VList.prototype.width=300;VList.prototype.height=600;VList.prototype.items=null;VList.prototype.need_scroll=false;VList.prototype.mask_top=null;VList.prototype.mask_bottom=null;
VList.prototype.start=0;VList.prototype.end=0;
VList.prototype.focus_moveto=null;VList.prototype.data_list=null;VList.prototype.item_class=SimpleItem;VList.prototype.item_margin=2;
VList.prototype.setDataList=function(data_list){
this.cursor=0;

this.data_list=data_list;
this.components=[];
this.items=[];
this.start=0;
this.end=0;
this.mask_top.visible_p=false;
this.mask_bottom.visible_p=false;
for(var i=0;i<this.data_list.length;i++){
var tmp=new this.item_class();
this.$setDepthTestTrue(tmp);
tmp.setData(this.data_list[i]);
this.items.push(tmp);
this.components.push(tmp);}
this.need_scroll=this.$correctPosition();};
VList.prototype.$setDepthTestTrue=function(cont){
for(var i=0;i<cont.components.length;i++){
if(cont.components[i] instanceof container){
this.$setDepthTestTrue(cont.components[i]);}else{
cont.components[i].depth_test=true;}
}
};
VList.prototype.$correctPosition=function(is_from_bottom){
if(is_from_bottom){
return this.$correctPositionBottom();}else{
return this.$correctPositionTop();}
};
VList.prototype.$correctPositionTop=function(){
if(this.items.length==0)return;
var pos=this.height/2-this.item_margin;
var item=null;
this.components=[
this.mask_top,this.mask_bottom
];
if(this.start!=0){
this.start=this.cursor-1;}
for(var i=this.start;i<this.items.length;i++){
item=this.items[i];
item.translate[1]=pos-((i==0)?0:item.getTopMargin())-item.getHeight()/2;
pos=item.translate[1]-item.getHeight()/2-this.item_margin;
this.components.push(item);
if(pos<-this.height/2){
this.end=i;
this.mask_bottom.translate[1]=-(this.height/2+this.items[i].getHeight()/2);
this.mask_bottom.width=this.items[i].getWidth()+4;
this.mask_bottom.height=this.items[i].getHeight();
this.mask_bottom.visible_p=true;
this.mask_top.visible_p=false;
return true;}
}
return false;};
VList.prototype.$correctPositionBottom=function(){
if(this.items.length==0)return;
var pos=-this.height/2+this.item_margin;
var item=null;
this.components=[
this.mask_top,this.mask_bottom
];
this.end=this.cursor+1;
for(var i=this.end;i>=0;i--){
item=this.items[i];
item.translate[1]=pos+item.getHeight()/2;
pos=item.translate[1]+item.getHeight()/2+this.item_margin+item.getTopMargin();
this.components.push(item);
if(pos>this.height/2){
this.start=i;
this.mask_top.translate[1]=this.height/2+this.items[i].getHeight()/2;
this.mask_top.width=this.items[i].getWidth()+4;
this.mask_top.height=this.items[i].getHeight();
this.mask_top.visible_p=true;
this.mask_bottom.visible_p=false;
break;}
}
};
VList.prototype.setItemMargin=function(margin){
this.item_margin=margin;};
VList.prototype.getSelectedIndex=function(){
return this.cursor;};
VList.prototype.getSelectedItem=function(){
return this.items[this.cursor];};
VList.prototype.setVisible=function(b){
this.visible_p=b;};
VList.prototype.getVisible=function(){
return this.visible_p;};
VList.prototype.onFocus=function(b){
this.items[this.cursor].onFocus(b);
if(b==true){
this.$correctFocus();}
};
VList.prototype.setTranslate=function(x,y,z){
if(isNumber(x))this.translate[0]=x;
if(isNumber(y))this.translate[1]=y;
if(isNumber(z))this.translate[2]=z;};
VList.prototype.getItemsHeight=function(){
var it=this.components[this.components.length-1];
var end_y=it.translate[1]-it.getHeight()/2-this.item_margin;
return Math.min(this.height/2-end_y,this.height)
};
VList.prototype.getSize=function(){
return [this.width,this.height];};
VList.prototype.setSize=function(w,h){
this.width=w;
this.height=h;
this.mask_top.width=w+4;
this.mask_bottom.width=w+4;
this.mask_top.visible_p=false;
this.mask_bottom.visible_p=false;
this.need_scroll=this.$correctPosition();};
VList.prototype.key_hook=function(updown,keysym){
if(updown!=KEY_PRESS)return false;
switch(keysym){
case TXK_ENTER:
return this.items[this.cursor].action(this);
case TXK_DOWN:
if(this.cursor==this.items.length-1){
break;}else{
this.items[this.cursor].onFocus(false);
this.cursor++;
if((this.need_scroll)&&(this.cursor>=this.end-1)&&(this.cursor!=this.items.length-1)){
this.$correctPosition(true);}
this.items[this.cursor].onFocus(true);
this.$correctFocus();
return true
}
case TXK_UP:
if(this.cursor==0){
break;}else{
this.items[this.cursor].onFocus(false);
this.cursor--;
if((this.need_scroll)&&(this.cursor<=this.start+1)&&(this.cursor!=0)){
this.$correctPosition(false);}
this.items[this.cursor].onFocus(true);
this.$correctFocus();
return true;}
default:
}
return false;};
VList.prototype.$correctFocus=function(){
var item=this.items[this.cursor];
this.focus_moveto(this.translate.map(function(v,i,ary){return v+item.translate[i];}),
[item.getWidth()+4,item.getHeight()+4],
0);};
function FriendItem(data){
this.superClass=container;
this.superClass({
components:[
this.back=new gbox({width:this.WIDTH,height:this.HEIGHT,
color:this.BACKGROUND_COLOR}),
this.face=new gimage({width:150/2,height:150/2,
translate:[-(this.WIDTH-144)/2,0,0],}),
this.nickname=new gtext({width:this.NICKNAME_WIDTH,omitter:"...",
color:this.NICKNAME_COLOR,
font_size:this.NICKNAME_FONT_SIZE,
translate:[((this.WIDTH/2-110/2)-530/4),0,0]}),
new gbox({color:this.MARK_BACK_COLOR,
translate:[(this.WIDTH-56)/2,0,0],
width:56,height:this.HEIGHT}),
this.mark=new gimage({width:56,height:this.HEIGHT,
translate:[(this.WIDTH-56)/2,0,0],}),
]
});

if(data){
this.setData(data);}
}
FriendItem.prototype=new container({});
FriendItem.prototype.BACKGROUND_COLOR=[220,220,220,255];FriendItem.prototype.MARK_BACK_COLOR=[190,190,190,255];FriendItem.prototype.NICKNAME_COLOR=[0,0,0,255];FriendItem.prototype.NICKNAME_FONT_SIZE=28;FriendItem.prototype.NICKNAME_WIDTH=530/2;
FriendItem.prototype.WIDTH=900/2;FriendItem.prototype.HEIGHT=176/2;FriendItem.prototype.TOP_MARGIN=0;
FriendItem.prototype.data=null;FriendItem.prototype.nickname=null;FriendItem.prototype.back=null;FriendItem.prototype.face=null;FriendItem.prototype.mark=null;
FriendItem.prototype.toString=function(){
return ""+(this.data?this.data.nickname:"No Name");};
FriendItem.prototype.getTopMargin=function(){
return this.TOP_MARGIN;};
FriendItem.prototype.getWidth=function(){
return this.WIDTH;};
FriendItem.prototype.getHeight=function(){
return this.HEIGHT;};
FriendItem.prototype.getData=function(){
return this.data;};
FriendItem.prototype.setData=function(data){
this.data=data;
setf_text(this.nickname,data.nickname);
this.face.src=data.face;};
FriendItem.prototype.setMark=function(img_src){
if(!img_src){
this.mark.src="";}else{
this.mark.src=img_src;}
};
FriendItem.prototype.action=function(){
return false;};
FriendItem.prototype.onFocus=function(b){
return b;};

function FriendListGUI(focus_moveto,action_func){
this.superClass=VList;
this.superClass(focus_moveto,FriendItem);
this.back=new gbox({width:this.width,height:this.height,
visible_p:false,
color:[255,25,25,255]});
if(action_func){
this.action_func=action_func;}
this.setDataList([{id:null,nickname:"Now Loading ...",face:""}]);}
FriendListGUI.prototype=new VList;
FriendListGUI.prototype.width=450;FriendListGUI.prototype.height=1688/2;FriendListGUI.prototype.back=null;
FriendListGUI.prototype.action_func=null;FriendListGUI.prototype.filter_func=null;FriendListGUI.prototype.item_margin=2;
FriendListGUI.prototype.setDataList=function(fd_list){
VList.prototype.setDataList.apply(this,arguments);
this.components.unshift(this.back);};
FriendListGUI.prototype.$correctPosition=function(is_from_bottom){
var ret=VList.prototype.$correctPosition.apply(this,arguments);
this.components.unshift(this.back);
return ret;};
FriendListGUI.prototype.setActionFunc=function(action_func){
this.action_func=action_func;};
FriendListGUI.prototype.setFilter=function(filter_func){
this.filter_func=filter_func;
this.applyFilter();};
FriendListGUI.prototype.applyFilter=function(){
if(this.filter_func instanceof Function){
var func=this.filter_func;
this.items.forEach(function(v){v.setMark(func(v.getData()));});}else{
this.items.forEach(function(v){v.setMark("");});}
};
FriendListGUI.prototype.setSize=function(w,h){
VList.prototype.setSize.apply(this,arguments);
this.back.width=w;
this.back.height=h;};
FriendListGUI.prototype.key_hook=function(updown,keysym){
if(updown!=KEY_PRESS)return false;
var ret=VList.prototype.key_hook.apply(this,arguments);
if(ret==false){
try{
switch(keysym){
case TXK_ENTER:
this.action_func(this.items[this.cursor].getData());
return true;
case TXK_RETURN:
this.action_func(null);
break;
default:
}
}catch(e){
console.log("@ FriendListGUI.prototype.key_hook : "+e.message);
}
}
return ret;};

provide("pkg_friends");
