import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/base/YSTabBar.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/login/YSWalletSetting.dart';
import 'package:flutter_wallet/tools/YSColors.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:video_player/video_player.dart';

import '../tools/YSAlertView.dart';
import '../tools/YSNetWork.dart';
import '../tools/YSTools.dart';

class YSLoginAuth extends StatefulWidget {
  const YSLoginAuth({Key? key}) : super(key: key);

  @override
  YSLoginAuthState createState() => YSLoginAuthState();
}

class YSLoginAuthState extends State<YSLoginAuth> {
  late VideoPlayerController _controller;
  bool _isCheck = false;
  bool _isLoad = false;
  bool _isLogin = false;
  @override
  void initState() {
    _controller = VideoPlayerController.asset('images/yv1.mp4')..initialize().then((_) {});
    _checkLogin();
    _isLoad = true;
    // _getVersionData();
    super.initState();
  }
  _getVersionData() async{
    YSNetWork.ysRequestHttp(context, type: RequestType.post, api: 'versions', parameter: {}, successSetter: (dict) async{
      List list = dict['data']['list'];
      _isLoad = true;
      setState(() {});
      if(list.isNotEmpty){
        Map data = list.first;
        // PackageInfo packageInfo = await PackageInfo.fromPlatform();
        //int.parse(packageInfo.buildNumber)<data['build']&&data['is_force']==1;
        getVersionData(context,data);
      }
    });
  }

  _checkLogin() async{
    String word = await YSSharedPreferences.getStringValue('word')??'';
    LogUtil.d('_checkLogin======$word');
    _isLogin = word.isNotEmpty;
  }

  _authCheck() async{
    if(YSUserRecord().lock=='1'){
      bool isCheck =  await YSAuthCheck().check();
      if(isCheck&&mounted){
        _intoPage();
      }
    }else{
      _intoPage();
    }
  }
  _intoPage() {
    _controller.play();
    Future.delayed(Duration(seconds: _controller.value.duration.inSeconds)).then((value) {
      _isCheck = false;
      Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context){
        return _isLogin?const YSTabBar():const YSWalletSetting();
      }), (route) => false);
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Container(
        padding: EdgeInsets.only(top: ysTOP(context)+hsp(20),left: hsp(15),right: hsp(15)),
        width: ysWidth(context),
        height: ysHeight(context),
        child: Column(
          children: [
            Image.asset(
              YSColors.imageStyle(context, '资源 3'),
              width: hsp(150),
              height: hsp(30),
              color: Colors.white,
            ),
            Expanded(child: Center(
              child: SizedBox(
                height: hsp(400),
                width: ysWidth(context),
                child: VideoPlayer(_controller),
              ),
            )),
            if(_isLoad)GestureDetector(
              onTap: (){
                if(_isCheck==false){
                  _isCheck = true;
                  _authCheck();
                }
              },
              behavior: HitTestBehavior.opaque,
              child: Container(
                margin: EdgeInsets.only(bottom: hsp(30)),
                height: hsp(45),
                width: hsp(230),
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(5))
                ),
                alignment: Alignment.center,
                child: Text(S.current.KAISHISHIYONG,style: TextStyle(fontSize: zsp(18),color: Colors.black,fontWeight: FontWeight.bold),),
              ),
            )
          ],
        ),
      ),
    );
  }
}
