/*
 * @Author: wjc
 * @Date: 2022-04-14 16:42:10
 * @LastEditors: wjc
 * @LastEditTime: 2023-04-03 10:42:06
 * @Description: 
 */
// import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:smart_devices_package/src/components/showCofirmMeterDialog.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/private_sd_index_model.dart';

class IndexPage extends StatelessWidget {
  const IndexPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (providerContext) =>
          PrivateSdIndexStore(context, providerContext),
      child: _PageView(),
      lazy: false,
    );
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PrivateSdIndexStore>(context);
    final list = store.smartDevicesList;
    final colorScheme = WTheme.of(context).colorScheme;

    return Theme(
        data: Theme.of(
          context,
        ).copyWith(
          primaryColorLight: Color(0xff505050),
        ),
        child: GestureDetector(
          onTap: () {
            FocusScope.of(context).requestFocus(FocusNode());
          },
          child: Scaffold(
            appBar: WAppBar(
              brightness: Brightness.light,
              iconTheme: IconThemeData(
                color: colorScheme.mainTitle,
                size: 19.pt,
              ),
              backgroundColor: Colors.white,
              title: WisText('${store.routingArgs['appBarTitle'] ?? '智能设备'}',
                  size: 19.pt,
                  weight: FontWeight.w600,
                  color: colorScheme.mainTitle),
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              mainAxisSize: MainAxisSize.min,
              children: [
                Wisdom.row(
                    color: Color(0xFFFFFFFF),
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    margin: EdgeInsets.only(bottom: 5.pt),
                    children: <Widget>[
                      Expanded(
                        child: WSearch(
                          hintText: '输入设备名称搜索',
                          onSubmitted: (value) => store.onNameInput(value),
                          onCancel: () => store.onNameInput(''),
                          moonlight: false,
                          padding: EdgeInsets.symmetric(vertical: 10.pt),
                          isDark: true,
                        ),
                      )
                    ]),
                WisScrollView(
                  key: store.scorllViewListKey,
                  onInit: () => store.getSmartDevicesList(isLoad: false),
                  onLoad: () => store.getSmartDevicesList(isLoad: true),
                  slivers: <Widget>[
                    WisSliverList.childBuilder(
                      childCount: list.length,
                      builder: (context, index) {
                        final item = list[index];
                        return Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: <Widget>[
                            _MeterItem(item: item),
                          ],
                        );
                      },
                    ),
                  ],
                ).asExpanded(),
              ],
            ),
          ),
        ));
  }
}

/// 智能表
class _MeterItem extends StatelessWidget {
  const _MeterItem({Key? key, this.item}) : super(key: key);
  final item;
  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final store = Provider.of<PrivateSdIndexStore>(context);
    dynamic controllerData = {'controllerId': item?.controllerId};
    return Wisdom(
      onTap: () => store.routeToDetail(item),
      margin: EdgeInsets.symmetric(horizontal: 10.pt, vertical: 5.pt),
      color: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: 15.pt, vertical: 10.pt),
      borderRadius: BorderRadius.circular(12.pt),
      boxShadow: [
        BoxShadow(
          offset: Offset(0, 2),
          color: Color(0x08000000),
          blurRadius: 10.pt,
          spreadRadius: 2.5.pt,
        ),
      ],
      child: Wisdom.column(
        mainAxisSize: MainAxisSize.min,
        color: Colors.white,
        children: [
          Wisdom.column(
            mainAxisSize: MainAxisSize.min,
            // padding: EdgeInsets.all(15.pt),
            children: [
              Wisdom.row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                // margin: EdgeInsets.only(bottom: 5.pt),
                children: [
                  Wisdom.row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Wisdom(
                        child: Text(
                          '${item?.name ?? '--'}',
                          style: TextStyle(
                            color: colorScheme.mainTitle,
                            fontSize: 15.pt,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ).asExpanded(),
                ],
              ),
              Wisdom.row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Wisdom(
                    margin: EdgeInsets.only(right: 5.pt),
                    child: Text(
                      '区域位置：${item?.area ?? '--'}',
                      style: TextStyle(
                        color: colorScheme.minor,
                        fontSize: 13.pt,
                        fontWeight: FontWeight.w300,
                      ),
                    ),
                  ).asExpanded(),
                  if (item?.status != null && item.status == 0)
                    Switch(
                      value: false,
                      activeColor: colorScheme.primary,
                      onChanged: (v) => {
                        ShowConfirmMeterDialog(
                            context, controllerData, store, 'OPEN')
                      },
                    ),
                  if (item?.status != null && item.status == 1)
                    Switch(
                      activeColor: colorScheme.primary,
                      value: true,
                      onChanged: (v) => {
                        ShowConfirmMeterDialog(
                            context, controllerData, store, 'CLOSE')
                      },
                    ),
                ],
              ),
              Wisdom.row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Wisdom(
                    margin: EdgeInsets.only(right: 5.pt),
                    child: Text(
                      '状态：',
                      style: TextStyle(
                        color: colorScheme.minor,
                        fontSize: 13.pt,
                        fontWeight: FontWeight.w300,
                      ),
                    ),
                  ),
                  if (item?.status != null && item.status == 0)
                    WTag(
                      child: Text('已断电'),
                      color: Color(0xffF95759),
                      margin: EdgeInsets.only(left: 5.pt),
                    ),
                  if (item?.status != null && item.status == 1)
                    WTag(
                      child: Text('已通电'),
                      color: Color(0xff25B41C),
                      margin: EdgeInsets.only(left: 5.pt),
                    ),
                ],
              )
            ],
          ),
        ],
      ),
    );
  }
}
