import 'dart:developer';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../plugin/C.dart';
import '../../plugin/HTTP.dart';

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

  @override
  State<TargetLog> createState() => _TargetLogState();
}

class _TargetLogState extends State<TargetLog> {
  String log = "";
  bool isLoading = true; // ✅ 新增：是否正在加载

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

  Future<void> _getTargetLog() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    int _currentVersionCode = int.tryParse(packageInfo.buildNumber) ?? 0;

    try {
      dynamic json = await HTTP.create("${C.BASE_URL}/server/version/$_currentVersionCode").execute();

      if (json["code"] != 200) {
        setState(() {
          log = json["message"];
          isLoading = false; // ✅ 加载完成
        });
      } else {
        setState(() {
          log = json["data"]["updateInfo"];
          isLoading = false; // ✅ 加载完成
        });
      }
    } catch (_) {
      setState(() {
        log = "网络问题，请重试...";
        isLoading = false; // ✅ 加载完成
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.all(10),
      child: isLoading
          ? const SizedBox(
        width: 24,
        height: 24,
        child: CircularProgressIndicator(strokeWidth: 2),
      ) // ✅ 加载圈
          : Text(
        log,
        style: TextStyle(color: Colors.grey[600], fontSize: 14),
      ),
    );
  }
}

// 自定义无边缘阴影 ScrollBehavior
class NoGlowScrollBehavior extends ScrollBehavior {
  @override
  Widget buildViewportChrome(
      BuildContext context,
      Widget child,
      AxisDirection axisDirection,
      ) {
    return child;
  }

  @override
  Widget buildOverscrollIndicator(
      BuildContext context,
      Widget child,
      ScrollableDetails details,
      ) {
    return child;
  }
}
