package admin.conn
{
    import admin.event.*;
    import admin.model.*;
    import com.webgame.as3.events.*;
    import com.webgame.as3.socket.*;
    import flash.events.*;

    public class AdminConn extends Object
    {
        private var adminSocket:GameSocket = null;
        private var host:String = "landong4.dns21.org";
        private var port:int = 6301;
        public var isConnect:Boolean = false;
        private static var _instance:AdminConn = null;

        public function AdminConn()
        {
            if (!this.adminSocket)
            {
                this.adminSocket = new GameSocket(this.host, this.port);
                this.adminSocket.addEventListener(Event.CONNECT, this.connectHandler);
                this.adminSocket.addEventListener(SocketDataEvent.SOCKET_DATA_RECV_EVENT, this.revDataHandler);
                this.adminSocket.addEventListener(SocketDataEvent.SOCKET_CONNECT_FAIL_EVENT, this.connectionFailHandler);
                this.adminSocket.addEventListener(SocketDataEvent.SOCKET_CONNECT_CLOSE_EVENT, this.connCloseHandler);
                this.adminSocket.addEventListener(SocketDataEvent.SOCKET_SEND_FAIL_EVENT, this.sendErrHandler);
            }
            return;
        }// end function

        public function send(param1:Object) : void
        {
            if (this.adminSocket)
            {
            }
            if (!param1)
            {
                return;
            }
            this.adminSocket.send(param1);
            return;
        }// end function

        private function connectHandler(event:Event) : void
        {
            trace("connected");
            this.isConnect = true;
            EventProxy.instance.dispatchEvent(event);
            return;
        }// end function

        private function revDataHandler(event:SocketDataEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:Object = null;
            var _loc_6:GoodState = null;
            var _loc_7:Array = null;
            var _loc_8:int = 0;
            var _loc_9:Object = null;
            var _loc_10:GoodState = null;
            var _loc_11:Array = null;
            var _loc_12:int = 0;
            var _loc_13:Object = null;
            var _loc_14:LogInfo = null;
            var _loc_2:* = event.sockData;
            if (!_loc_2)
            {
                return;
            }
            switch(_loc_2.cmd)
            {
                case 268500993:
                {
                    UserModel.instance.charId = _loc_2.CharId;
                    UserModel.instance.code = _loc_2.Result;
                    EventProxy.instance.dispatchEvent(new AdminEvent(AdminEvent.USER_REG_DATA_EVENT));
                    break;
                }
                case 268500994:
                {
                    UserModel.instance.charId = _loc_2.CharId;
                    UserModel.instance.baseTime = _loc_2.BaseTime;
                    UserModel.instance.sign = _loc_2.Sign;
                    UserModel.instance.code = _loc_2.Result;
                    if (_loc_2.Line)
                    {
                        _loc_3 = new Array();
                        _loc_4 = 0;
                        while (_loc_4 < _loc_2.Line.length)
                        {
                            
                            _loc_5 = _loc_2.Line[_loc_4];
                            _loc_6 = new GoodState();
                            _loc_6.no = _loc_5.lineno;
                            _loc_6.expireTime = _loc_5.expireTime;
                            _loc_3.push(_loc_6);
                            _loc_4 = _loc_4 + 1;
                        }
                        UserModel.instance.lines = _loc_3;
                    }
                    EventProxy.instance.dispatchEvent(new AdminEvent(AdminEvent.USER_LOGIN_DATA_EVENT));
                    break;
                }
                case 268500995:
                {
                    GoodsListModel.instance.goodsList = _loc_2.GoodList;
                    EventProxy.instance.dispatchEvent(new AdminEvent(AdminEvent.GOODS_LIST_DATA_EVENT));
                    break;
                }
                case 268500996:
                {
                    UserModel.instance.code = _loc_2.Result;
                    if (_loc_2.Line)
                    {
                        _loc_7 = new Array();
                        _loc_8 = 0;
                        while (_loc_8 < _loc_2.Line.length)
                        {
                            
                            _loc_9 = _loc_2.Line[_loc_8];
                            _loc_10 = new GoodState();
                            _loc_10.no = _loc_9.lineno;
                            _loc_10.expireTime = _loc_9.expireTime;
                            _loc_7.push(_loc_10);
                            _loc_8 = _loc_8 + 1;
                        }
                        UserModel.instance.lines = _loc_7;
                    }
                    EventProxy.instance.dispatchEvent(new AdminEvent(AdminEvent.CHARGE_DO_DATA_EVENT));
                    break;
                }
                case 268500997:
                {
                    UserModel.instance.code = _loc_2.Result;
                    if (_loc_2.Log)
                    {
                        _loc_11 = new Array();
                        _loc_12 = 0;
                        while (_loc_12 < _loc_2.Log.length)
                        {
                            
                            _loc_13 = _loc_2.Log[_loc_12];
                            _loc_14 = new LogInfo();
                            _loc_14.logAward = _loc_13.Award;
                            _loc_14.logCode = _loc_13.Code;
                            _loc_14.logMonths = _loc_13.Months;
                            _loc_14.logPrice = _loc_13.Price;
                            _loc_14.logTime = _loc_13.Time;
                            _loc_11.push(_loc_14);
                            _loc_12 = _loc_12 + 1;
                        }
                        UserModel.instance.logs = _loc_11;
                    }
                    EventProxy.instance.dispatchEvent(new AdminEvent(AdminEvent.CHARGE_LOG_DATA_EVENT));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function connectionFailHandler(event:SocketDataEvent) : void
        {
            var _loc_2:* = new AdminEvent(AdminEvent.SOCKET_CONNECT_FAIL_EVENT);
            _loc_2.errMsg = event.errtext;
            EventProxy.instance.dispatchEvent(_loc_2);
            return;
        }// end function

        private function connCloseHandler(event:SocketDataEvent) : void
        {
            var _loc_2:* = new AdminEvent(AdminEvent.SOCKET_CLOSE_EVENT);
            _loc_2.errMsg = event.errtext;
            EventProxy.instance.dispatchEvent(_loc_2);
            return;
        }// end function

        private function sendErrHandler(event:SocketDataEvent) : void
        {
            trace("Admin conn sendErrHandler");
            return;
        }// end function

        public static function get instance() : AdminConn
        {
            return connect();
        }// end function

        public static function connect() : AdminConn
        {
            if (!_instance)
            {
                _instance = new AdminConn;
            }
            return _instance;
        }// end function

    }
}
