import 'dart:io';
import 'package:device_apps/device_apps.dart' as app;
import 'package:android_intent/android_intent.dart';
import 'package:android_intent/flag.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/pages/home/sport_list_page.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/button_primary.dart';

import 'image.dart';
import 'image.dart';
import 'image.dart';

Future startRun(BuildContext context) async {
  var package = "com.ouj.hiyd";
  var componentName = "com.ouj.hiyd.SplashActivity_";

  if (Platform.isAndroid) {
    var sport = await app.DeviceApps.getApp(package);
    if (sport == null) {
      ToastUtil.show("你还没安装 Hi运动 app");
      return;
    }
    AndroidIntent intent = AndroidIntent(
        action: "android.intent.action.MAIN",
        package: package,
        componentName: componentName,
        flags: [Flag.FLAG_ACTIVITY_NEW_TASK],
        arguments: {});
    intent.launch();
  }
}

class GameRun extends StatelessWidget {
  Function changeIndex;
  GameRun({this.changeIndex});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (changeIndex != null) {
          changeIndex(1);
        } else {
          showDialog(
              context: context,
              child: SportListPage(
                index: ValueNotifier(1),
              ));
        }
      },
      child: Row(
        children: <Widget>[
          Container(
              width: 50.0,
              height: 50.0,
              margin: const EdgeInsets.all(12.0),
              // child: Image.asset("lib/assets/img/home_game_run.png"),
              child: Image.asset("lib/assets/img/game_icon_function.png")),
          // Expanded(
          //   child: Column(
          //     crossAxisAlignment: CrossAxisAlignment.start,
          //     children: <Widget>[
          // Text(
          //   "跑步训练",
          //   style: Theme.of(context)
          //       .textTheme
          //       .subtitle1
          //       .copyWith(fontSize: 16.0, fontWeight: FontWeight.w600),
          // ),
          // SizedBox(
          //   height: 3,
          // ),
          // Text("125514人在练",
          //     style: Theme.of(context).textTheme.bodyText1, maxLines: 1),
          // SizedBox(
          //   height: 6,
          // )
          //     ],
          //   ),
          // ),
          // Padding(
          //   padding: const EdgeInsets.all(12.0),
          //   child: PrimaryButton(
          //     width: 93,
          //     height: 35,
          //     content: "开始跑步",
          //     callback: () {
          //       // startRun(context);
          //     },
          //   ),
          // )
          Text(
            "小功能合集",
            style: Theme.of(context)
                .textTheme
                .subtitle1
                .copyWith(fontSize: 16.0, fontWeight: FontWeight.w600),
          ),
          Expanded(
              child: Padding(
            padding: EdgeInsets.only(right: 12.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[arrowRight4()],
            ),
          ))
        ],
      ),
    );
  }
}

class FunctionSet extends StatelessWidget {
  List map = [
    {
      "name": "户外跑步",
      "url": "function_icon_run.png",
      "callBack": () {},
    },
    {
      "name": "实时计步",
      "url": "function_icon_steps.png",
      "callBack": () {},
    },
    {
      "name": "体重检测",
      "url": "function_icon_weight.png",
      "callBack": () {},
    }
  ];

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Padding(
        padding: EdgeInsets.fromLTRB(15.0, 0, 15.0, 0),
        child: GridView(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          padding: EdgeInsets.zero,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            crossAxisSpacing: 12.0,
          ),
          children: map
              .map((e) => GestureDetector(
                    onTap: () {
                      e['callBack']();
                    },
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.all(Radius.circular(5.0))),
                      child: Column(
                        children: <Widget>[
                          Padding(
                            padding: EdgeInsets.fromLTRB(22.0, 12.0, 22.0, 8.0),
                            child: Image.asset(
                              "lib/assets/img/${e["url"]}",
                              width: 56.0,
                              height: 56.0,
                            ),
                          ),
                          Text(
                            "${e['name']}",
                            style: TextStyle(color: Color(0xff000000)),
                          )
                        ],
                      ),
                    ),
                  ))
              .toList(),
          // itemBuilder: (context, index) {
          //   return Container(
          //     decoration: BoxDecoration(
          //         color: Colors.white,
          //         borderRadius: BorderRadius.all(Radius.circular(5.0))),
          //     height: 116,
          //     child: Column(
          //       children: <Widget>[
          //         Padding(
          //           padding: EdgeInsets.fromLTRB(22.0, 12.0, 22.0, 8.0),
          //           child: Image.asset(
          //             "lib/assets/img/function_icon_run.png",
          //             width: 56.0,
          //             height: 56.0,
          //           ),
          //         ),
          //         Text(
          //           "户外跑步",
          //           style: TextStyle(color: Color(0xff000000)),
          //         )
          //       ],
          //     ),
          //   );
          // },
          // itemCount: 3,
        ),
      ),
    );
  }
}
