import { asyncDelay, buttonOnClick, onHomeworkFinish } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {

  onLoadEnd() {
    this.initData();
    this.initListener();
  }

  middleLayer;
  initData() {
    const middleLayer = cc.find('middleLayer')?.getComponent('middleLayer');
    if (middleLayer) {
      this.middleLayer = middleLayer;
    }
  }

  async login(phone, password, passCode = '') {
    if (!this.checkPhone(phone)) {
      console.log('请输入正确的手机号码');
      return;
    }

    const check = cc.find('Canvas/bg/ui/btnLaw/check');
    if (!check.active) {
      console.log('请勾选下方协议');
      return;
    }

    const res = await this.middleLayer.asyncCallNetworkApiPost('/api/oxford/v1/login', {
      phone: phone,
      password: password,
      sms_code: passCode
    });
    console.log('res = ' + JSON.stringify(res));
  }

  checkPhone(phone) {
    const test = /^[1]([3-9])[0-9]{9}$/;
    if (!test.test(phone)) {
      return false;
    }
    return true;
  }

  async sendCode(phone) {
    if (!this.checkPhone(phone)) {
      console.log('请输入正确的手机号码');
      return;
    }
    const res = await this.middleLayer.asyncCallNetworkApiPost('/api/oxford/v1/sendcode', {
      phone: phone
    });
    console.log('res = ' + JSON.stringify(res));
  }

  initListener() {
    const btnLaw = cc.find('Canvas/bg/ui/btnLaw');
    buttonOnClick(btnLaw, () => {
      const check = cc.find('Canvas/bg/ui/btnLaw/check');
      check.active = !check.active;
    }, 1.02);

    const btnLogin = cc.find('Canvas/bg/ui/loginNode/btnLogin');
    buttonOnClick(btnLogin, async () => {
      const inputPhone = cc.find('Canvas/bg/ui/loginNode/phone/inputPhone');
      const phone = inputPhone.getComponent(cc.EditBox).string;
      const inputPassword = cc.find('Canvas/bg/ui/loginNode/password/password');
      const password = inputPassword.getComponent(cc.EditBox).string;

      this.login(phone, password);
    });

    const btnResetPassword = cc.find('Canvas/bg/ui/forgetNode/btnResetPassword');
    buttonOnClick(btnResetPassword, async () => {
      const inputPhone = cc.find('Canvas/bg/ui/forgetNode/phone/inputPhone');
      const phone = inputPhone.getComponent(cc.EditBox).string;
      const inputPassword = cc.find('Canvas/bg/ui/forgetNode/password/password');
      const password = inputPassword.getComponent(cc.EditBox).string;
      const inputPassCode = cc.find('Canvas/bg/ui/forgetNode/passCode/passCode');
      const passCode = inputPassCode.getComponent(cc.EditBox).string;

      this.login(phone, password, passCode);
    });

    const btnRegister = cc.find('Canvas/bg/ui/registerNode/btnRegister');
    buttonOnClick(btnRegister, async () => {
      const inputPhone = cc.find('Canvas/bg/ui/registerNode/phone/inputPhone');
      const phone = inputPhone.getComponent(cc.EditBox).string;
      const inputPassword = cc.find('Canvas/bg/ui/registerNode/password/password');
      const password = inputPassword.getComponent(cc.EditBox).string;
      const inputPassCode = cc.find('Canvas/bg/ui/registerNode/passCode/passCode');
      const passCode = inputPassCode.getComponent(cc.EditBox).string;

      this.login(phone, password, passCode);
    });

    const btnSendCode = cc.find('Canvas/bg/ui/registerNode/passCode/btnSendCode');
    buttonOnClick(btnSendCode, async () => {
      const inputPhone = cc.find('Canvas/bg/ui/registerNode/phone/inputPhone');
      const phone = inputPhone.getComponent(cc.EditBox).string;

      const label = btnSendCode.getChildByName('label').getComponent(cc.Label);
      // 发送验证码
      if (label.string == '发送验证码') {
        let countDown = 10;
        const tween = cc.tween(label)
          .call(() => {
            label.string = `${countDown}`;
            if (countDown == 0) {
              label.string = '发送验证码';
              tween.stop();
            }
            countDown--;
          })
          .delay(1)
          .union()
          .repeatForever()
          .start();
      }
      this.sendCode(phone);
    });
    const btnSendCode2 = cc.find('Canvas/bg/ui/forgetNode/passCode/btnSendCode');
    buttonOnClick(btnSendCode2, async () => {
      const inputPhone = cc.find('Canvas/bg/ui/forgetNode/phone/inputPhone');
      const phone = inputPhone.getComponent(cc.EditBox).string;
      const label = btnSendCode2.getChildByName('label').getComponent(cc.Label);
      if (label.string == '发送验证码') {
        let countDown = 10;
        const tween = cc.tween(label)
          .call(() => {
            label.string = `${countDown}`;
            if (countDown == 0) {
              label.string = '发送验证码';
              tween.stop();
            }
            countDown--;
          })
          .delay(1)
          .union()
          .repeatForever()
          .start();
      }
      this.sendCode(phone);
    });

    const btnGotoRegister = cc.find('Canvas/bg/ui/loginNode/btnRegister');
    buttonOnClick(btnGotoRegister, () => {
      const loginNode = cc.find('Canvas/bg/ui/loginNode');
      const registerNode = cc.find('Canvas/bg/ui/registerNode');
      cc.tween(loginNode)
        .to(0.3, { scaleX: 0, x: 200 }, { easing: 'sineOut' })
        .call(() => {
          loginNode.active = false;
        })
        .start();
      registerNode.active = true;
      cc.tween(registerNode)
        .set({ scaleX: 0, x: -200 })
        .to(0.3, { scaleX: 1, x: 0 }, { easing: 'sineIn' })
        .start();
    });
    const btnForgetPassword = cc.find('Canvas/bg/ui/loginNode/btnForgetPassword');
    buttonOnClick(btnForgetPassword, () => {
      const loginNode = cc.find('Canvas/bg/ui/loginNode');
      const forgetNode = cc.find('Canvas/bg/ui/forgetNode');
      cc.tween(loginNode)
        .to(0.3, { scaleX: 0, x: -200 }, { easing: 'sineOut' })
        .call(() => {
          loginNode.active = false;
        })
        .start();
      forgetNode.active = true;
      cc.tween(forgetNode)
        .set({ scaleX: 0, x: 200 })
        .to(0.3, { scaleX: 1, x: 0 }, { easing: 'sineIn' })
        .start();
    });

    const btnGoToLogin = cc.find('Canvas/bg/ui/forgetNode/btnGoToLogin');
    buttonOnClick(btnGoToLogin, () => {
      const forgetNode = cc.find('Canvas/bg/ui/forgetNode');
      const loginNode = cc.find('Canvas/bg/ui/loginNode');
      cc.tween(forgetNode)
        .to(0.3, { scaleX: 0, x: -200 }, { easing: 'sineOut' })
        .call(() => {
          forgetNode.active = false;
        })
        .start();
      loginNode.active = true;
      cc.tween(loginNode)
        .set({ scaleX: 0, x: 200 })
        .to(0.3, { scaleX: 1, x: 0 }, { easing: 'sineIn' })
        .start();
    });
    const btnRegister2 = cc.find('Canvas/bg/ui/forgetNode/btnRegister');
    buttonOnClick(btnRegister2, () => {
      const forgetNode = cc.find('Canvas/bg/ui/forgetNode');
      const registerNode = cc.find('Canvas/bg/ui/registerNode');
      cc.tween(forgetNode)
        .to(0.3, { scaleX: 0, x: 200 }, { easing: 'sineOut' })
        .call(() => {
          forgetNode.active = false;
        })
        .start();
      registerNode.active = true;
      cc.tween(registerNode)
        .set({ scaleX: 0, x: -200 })
        .to(0.3, { scaleX: 1, x: 0 }, { easing: 'sineIn' })
        .start();
    });

    const btnGoToLogin2 = cc.find('Canvas/bg/ui/registerNode/btnGoToLogin');
    buttonOnClick(btnGoToLogin2, () => {
      const registerNode = cc.find('Canvas/bg/ui/registerNode');
      const loginNode = cc.find('Canvas/bg/ui/loginNode');
      cc.tween(registerNode)
        .to(0.3, { scaleX: 0, x: 200 }, { easing: 'sineOut' })
        .call(() => {
          registerNode.active = false;
        })
        .start();
      loginNode.active = true;
      cc.tween(loginNode)
        .set({ scaleX: 0, x: -200 })
        .to(0.3, { scaleX: 1, x: 0 }, { easing: 'sineIn' })
        .start();
    });
    const btnForgetPassword2 = cc.find('Canvas/bg/ui/registerNode/btnForgetPassword');
    buttonOnClick(btnForgetPassword2, () => {
      const registerNode = cc.find('Canvas/bg/ui/registerNode');
      const forgetNode = cc.find('Canvas/bg/ui/forgetNode');
      cc.tween(registerNode)
        .to(0.3, { scaleX: 0, x: -200 }, { easing: 'sineOut' })
        .call(() => {
          registerNode.active = false;
        })
        .start();
      forgetNode.active = true;
      cc.tween(forgetNode)
        .set({ scaleX: 0, x: 200 })
        .to(0.3, { scaleX: 1, x: 0 }, { easing: 'sineIn' })
        .start();
    });
  }
}
