import 'dart:async';

import 'package:flutter/material.dart';
import 'package:liftmanager/internal/bbs/model/brand_model_type.dart';
import 'package:liftmanager/internal/means/means_router.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/widgets/load_image.dart';

class BrandList extends StatefulWidget {
  @override
  BrandListState createState() => BrandListState();
}

class BrandListState extends State<BrandList> {
  ScrollController _scrollController = new ScrollController();
  List<ListBrand> brandList = [];
  var _controller = ScrollController();
  static const cellHeight = 70.0;
  static const dividerHeight = 1.0;
  Map<String, double> positionAnchors = {};

  @override
  void initState() {
    super.initState();
    _fetchData(null);
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: SearchAppBar(onPressed: () {
        NavigatorUtils.push(context, SearchRouter.searchBrand);
      }),
      body: Stack(alignment: Alignment.center, children: [
        ListView.separated(
          padding: EdgeInsets.symmetric(horizontal: 10),
          itemCount: brandList.length,
          separatorBuilder: (context, index) => Divider(
            height: dividerHeight,
          ),
          controller: _controller,
          itemBuilder: (_, index) {
            return GestureDetector(
              child: Container(
                height: cellHeight,
                // alignment: Alignment.centerLeft,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    LoadNetworkImageAlt(
                      brandList[index].logo,
                      width: 46,
                      height: 46,
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          brandList[index].name,
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                          ),
                        ),
                        Text(
                          brandList[index].descr ?? '',
                          style: TextStyle(
                            color: Color(0xff999999),
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              onTap: () {
                NavigatorUtils.push(context,
                    "${MeansRouter.brandDetail}?id=${brandList[index].id.toString()}");
              },
            );
          },
        ),
        Positioned(
          right: 0,
          child: AlphabeticIndexBar(controller: _controller, positionAnchors: positionAnchors,),
        ),
      ]),
    );
  }

  Future _fetchData(String keywords) async {
    NewApiService().getBrandListType(
      keywords,
      onSuccess: (res) {
        double offset = 0;
        for (BrandModelType bm in res) {
          if (bm.name == '热门') {
            continue;
          }

          brandList.addAll(bm.listBrand);
          positionAnchors[bm.name] = offset;
          offset += (cellHeight + dividerHeight) * bm.listBrand.length;
        }

        setState(() {});
      },
      onError: (int code, String msg) {},
    );
  }

}
