import 'package:food_health/api/endpoints/user_api.dart';
import 'package:food_health/api/network/safe.dart';
import 'package:food_health/page/system/login/widget/widget.dart';
import 'package:food_health/router/config/route_paths.dart';
import 'package:food_health/widgets/ui_kit/button/custom_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

import '../../../providers/app_store.dart';

class LoginCodePage extends StatefulWidget {
  final String email;
  final String password;

  const LoginCodePage({super.key, required this.email, required this.password});

  @override
  State<LoginCodePage> createState() => _LoginCodePageState();
}

class _LoginCodePageState extends State<LoginCodePage> {
  final _codeController = TextEditingController();
  var _subLoading = false;

  @override
  void initState() {
    super.initState();
    _handSendCode();
  }

  ///发送验证码
  void _handSendCode() {
    sendEmailCodeApi(widget.email);
    EasyLoading.showSuccess("Send success");
  }

  ///提交
  void _handSubmit() async {
    if (_codeController.text.isNotEmpty) {
      setState(() {
        _subLoading = true;
      });
      var res = await safeRequest(
        registerApi(
          widget.email,
          widget.password,
          _codeController.text,
        ),
        onError: (error) {
          setState(() {
            _subLoading = false;
          });
        },
      );
      var appStore = context.read<AppStore>();
      await appStore.setInfo(res);
      context.go(RoutePaths.layout);
      setState(() {
        _subLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.only(left: 20, right: 20, top: 40),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                "Check your inbox",
                style: Theme.of(context).textTheme.titleLarge,
              ),
              Container(
                margin: EdgeInsets.only(top: 20, bottom: 40),
                child: Text(
                  "Enter the verification code we just sent to ${widget.email}.",
                  textAlign: TextAlign.center,
                  style: Theme.of(context).textTheme.labelMedium,
                ),
              ),
              InputBox(hintText: "Code", controller: _codeController),
              Container(
                margin: EdgeInsets.only(top: 20),
                child: CustomButton(
                  loading: _subLoading,
                  onPressed: _handSubmit,
                  child: Text("Continue"),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 20),
                child: TextButton(
                  onPressed: () {
                    _handSendCode();
                  },
                  child: Text(
                    "Resend code",
                    style: Theme.of(context).textTheme.labelSmall,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
