import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:autosos_flutter/config/theme_colors.dart';
import 'package:autosos_flutter/const_config.dart';
import 'package:autosos_flutter/pages/home/widgets/image_block.dart';
import 'package:autosos_flutter/pages/home/widgets/map_widget.dart';
import 'package:autosos_flutter/pages/home/widgets/number_block.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool enabled = false;
  late AMapController _mapController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            automaticallyImplyLeading: false,
            title: const Text(
              "首页",
              style: TextStyle(color: Colors.white),
            ),
            actions: const [
              Text("今日在线时长？小时", style: TextStyle(color: Colors.white))
            ],
            flexibleSpace: Container(
              decoration: const BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("images/4.0x/home_top_bg.png"),
                      fit: BoxFit.cover)),
            )),
        backgroundColor: const Color(0xFFF3F3F3),
        body: PopScope(
          canPop: true, //能否向右滑动返回上一页
          child: Padding(
            padding: const EdgeInsets.all(10),
            child: Column(
              children: [
                buildTopWidget(),
                buildMiddleWidget(),
                const MapWidget()
              ],
            ),
          ),
        ));
  }

  Widget buildTopWidget() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(5))),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  enabled
                      ? const Text(
                          "接单中",
                          style: TextStyle(
                              fontSize: 23,
                              fontWeight: FontWeight.bold,
                              color: Colors.black),
                        )
                      : const Text(
                          "停止接单",
                          style: TextStyle(
                              fontSize: 23,
                              fontWeight: FontWeight.bold,
                              color: Colors.grey),
                        )
                ],
              ),
              Row(
                children: [
                  Switch(
                    value: enabled,
                    onChanged: (value) => setState(() {
                      enabled = value;
                    }),
                    activeColor: ThemeColors.primary,
                  ),
                  Container(
                    margin: const EdgeInsets.only(left: 10, right: 10),
                    width: 1,
                    height: 20,
                    color: Colors.grey,
                  ),
                  Column(
                    children: [
                      enabled
                          ? Image.asset("images/3.0x/order_receiving_star.png")
                          : Image.asset("images/3.0x/order_receiving_end.png"),
                      const Text("订单")
                    ],
                  )
                ],
              )
            ],
          ),
          GridView.count(
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            crossAxisCount: 3,
            childAspectRatio: 1.5,
            children: const [
              NumberBlock(title: "钱包(元)", number: "111"),
              NumberBlock(title: "总订单数", number: "111"),
              NumberBlock(title: "总金额(元)", number: "111"),
              ImageBlock(title: "提现", imgUrl: "images/3.0x/tixian_ic.png"),
              ImageBlock(
                  title: "个人中心", imgUrl: "images/3.0x/gerenzhongxin_ic.png"),
              ImageBlock(title: "设置", imgUrl: "images/3.0x/shezhi_ic.png"),
            ],
          ),
        ],
      ),
    );
  }

  Widget buildMiddleWidget() {
    return Container(
      margin: const EdgeInsets.only(top: 10),
      padding: const EdgeInsets.all(20),
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(5))),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  const Text(
                    "非事故拖车",
                    style: TextStyle(
                        color: ThemeColors.primary,
                        fontSize: 19,
                        fontWeight: FontWeight.bold),
                  ),
                  Container(
                      height: 20,
                      margin: const EdgeInsets.only(left: 10),
                      decoration: BoxDecoration(
                          border: Border.all(color: Colors.grey, width: 1),
                          borderRadius:
                              const BorderRadius.all(Radius.circular(10))),
                      child: const Padding(
                        padding: EdgeInsets.all(3),
                        child: Text(
                          "最近订单",
                          style: TextStyle(fontSize: 8),
                        ),
                      ))
                ],
              ),
              const Text("已完成")
            ],
          ),
          const Divider(),
          Row(
            children: [
              Container(
                margin: const EdgeInsets.only(right: 10),
                child: Image.asset("images/3.0x/time_ic.png"),
              ),
              const Text(
                "2024-01-23 16:36:03",
                style: TextStyle(fontSize: 12, color: Color(0xFF9B9B9B)),
              ),
            ],
          ),
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    Container(
                      margin: const EdgeInsets.only(right: 10),
                      child: const Icon(
                        Icons.circle,
                        size: 5,
                      ),
                    ),
                    const Text(
                      "宁波市鄞州区下应街道湖下路286号",
                      style: TextStyle(fontWeight: FontWeight.bold),
                    )
                  ],
                ),
                const Row(
                  crossAxisAlignment: CrossAxisAlignment.baseline,
                  textBaseline: TextBaseline.alphabetic,
                  children: [
                    Text(
                      "¥",
                      style: TextStyle(
                        fontSize: 13,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    Text(
                      "50",
                      style:
                          TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
                    ),
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget buildBottomWidget() {
    AMapWidget map = AMapWidget(
      ///必须正确设置的合规隐私声明，否则SDK不会工作，会造成地图白屏等问题。
      privacyStatement: ConstConfig.amapPrivacyStatement,
      apiKey: ConstConfig.amapApiKeys,
      mapType: MapType.navi,
      scaleEnabled: false,
      myLocationStyleOptions: MyLocationStyleOptions(
        true,
        circleFillColor: Colors.lightBlue,
        circleStrokeColor: Colors.blue,
        circleStrokeWidth: 1,
      ),
      onMapCreated: (AMapController controller) {
        setState(() {
          _mapController = controller;
          getApprovalNumber();
        });
      },
    );
    return Expanded(
        child: Stack(
      children: [
        Container(
          margin: const EdgeInsets.only(top: 10, bottom: 10),
          decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(5))),
          child: map,
        ),
        Positioned(
          bottom: 20,
          right: 20,
          child: Image.asset(
            "images/3.0x/current_location.png",
            width: 30,
            height: 30,
          ),
        )
      ],
    ));
  }

  ///获取审图号
  void getApprovalNumber() async {
    //普通地图审图号
    String? mapContentApprovalNumber =
        await _mapController.getMapContentApprovalNumber();
    //卫星地图审图号
    String? satelliteImageApprovalNumber =
        await _mapController.getSatelliteImageApprovalNumber();
    print('地图审图号（普通地图）: $mapContentApprovalNumber');
    print('地图审图号（卫星地图): $satelliteImageApprovalNumber');
  }
}
