const axios = require('axios').default;
const ClientNet = require('./clientnet');

function getTickCount() {
  return Math.floor((new Date()).getTime() / 1);
}

function httpGet(url, params) {
  return new Promise((resolve) => {
    const ret = {
      err: null,
      response: null,
    };
    axios({
      method: 'get',
      url: url,
      timeout: 1000 * 5,
      params: params,
      responseType: 'text'
    }).then((response) => {
      ret.response = response.data;
      resolve(ret);
    }).catch((error) => {
      ret.err = error;
      resolve(ret);
    });
  });
}

class TestCase {

  constructor(urls) {
    this.urls = urls;
    this.loginData = '';
    this.relationWs = new ClientNet(
      this.urls['relation'],
      'proto/friend/cs_proto.proto',
      'proto/friend/cs_msgid.proto'
    );
    console.log(this.urls['game2006']);
    this.battleWs = new ClientNet(
      this.urls['game2006'],
      'proto/battle/cs_proto.proto',
      'proto/battle/cs_msgid.proto'
    );
  }

  async init() {
    await this.step1();
    await this.step2();
    await this.step3();
    await this.step4();
    await this.step5();
    await this.step6();
    await this.step7();
    await this.step8();
    await this.step9();
    await this.step10();
    await this.step11();
  }

  async step1() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['login'] + '/webapp/index.php',
      {
        'c': 'Login',
        'a': 'auth',
        'gameid': 2006,
        'channel': 6513,
        'account': '123456',
        'openid': '123456'
      });
    this.loginData = response;
    const endTick = getTickCount();
    console.log('login auth@Login', endTick - startTick,  String(err), response);
  }

  async step2() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['cloud'] + '/webapp/index.php',
      {
        'c': 'Ops',
        'a': 'selfChecking',
      });
    const endTick = getTickCount();
    console.log('cloud selfChecking@Ops', endTick - startTick, String(err), response);
  }

  async step3() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['service'] + '/webapp/index.php',
      {
        'c': 'Ops',
        'a': 'selfChecking',
      });
    const endTick = getTickCount();
    console.log('service selfChecking@Ops', endTick - startTick, String(err), response);
  }

  async step4() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['gamelog'] + '/webapp/index.php',
      {
        'c': 'Ops',
        'a': 'selfChecking',
      });
    const endTick = getTickCount();
    console.log('gamelog selfChecking@Ops', endTick - startTick, String(err), response);
  }

  async step5() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['mail'] + '/webapp/index.php',
      {
        'c': 'Ops',
        'a': 'selfChecking',
      });
    const endTick = getTickCount();
    console.log('mail selfChecking@Ops', endTick - startTick, String(err), response);
  }

  async step6() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['game2006api'] + '/webapp/index.php',
      {
        'c': 'Ops',
        'a': 'selfChecking',
      });
    const endTick = getTickCount();
    console.log('game2006api selfChecking@Ops', endTick - startTick, String(err), response);
  }

  async step7() {
    await this.relationWs.init();
    await this.relationWs.connect();
    this.relationWs.on('connect', () => {
      console.log('relation onConnect');
      this.relationWs.registerMsgHandle('SMLogin', (msg) => {
        console.log(msg);
      });
      this.relationWs.sendMsg('CMLogin', {
        accountId: this.loginData['account_id'],
        sessionId: this.loginData['session_id'],
      });
    });
  }

  async step8() {
    await this.battleWs.init();
    await this.battleWs.connect();
    this.battleWs.on('connect', () => {
      console.log('battle onConnect');
      this.battleWs.registerMsgHandle('SMJoinedNotify', (msg) => {
        console.log(msg);
      });
      this.battleWs.sendMsg('CMJoin', {
        accountId: this.loginData['account_id'],
        sessionId: this.loginData['session_id'],
      });
    });
  }

  async step9() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['game2006'].replace('wss://', 'https://').
        replace('ws://', 'https://') +
        '/webapp/index.php',
      {
        'c': 'Ops',
        'a': 'getNodeId',
      });
    const endTick = getTickCount();
    console.log('game2006 getNodeId@Ops', endTick - startTick, String(err), response);
  }

  async step10() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['game2006-n1'].replace('wss://', 'https://').
        replace('ws://', 'https://') +
        '/webapp/index.php',
      {
        'c': 'Ops',
        'a': 'getNodeId',
      });
    const endTick = getTickCount();
    console.log('game2006-n1 getNodeId@Ops', endTick - startTick, String(err), response);
  }

  async step11() {
    const startTick = getTickCount();
    const {err, response} = await httpGet(
      this.urls['stat'] + '/webapp/index.php',
      {
        'c': 'Ops',
        'a': 'selfChecking',
      });
    const endTick = getTickCount();
    console.log('stat selfChecking@Ops', endTick - startTick, String(err), response);
  }

}

module.exports = TestCase;
