import 'package:ambito/src/config/config.dart';
import 'package:ambito/src/entity/business/business.dart';
import 'package:ambito/src/packages/ambito_theme/ambito_theme.dart';
import 'package:ambito/src/widgets/map/map_widget.dart';
import 'package:ambito/src/widgets/map/marker_generator.dart';
import 'package:ambito/src/widgets/page/base_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_geocoding_api/google_geocoding_api.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';
import 'package:screen_breakpoints/screen_breakpoints.dart';

import '../../consts/consts.dart';

part 'layouts/normal_layout_dashboard_page.dart';
part 'layouts/small_layout_dashboard_page.dart';
part 'parts/parts_dashboard_page.dart';

class DashboardPage extends StatefulWidget {
  const DashboardPage(
      {super.key, required this.businessId, required this.userId});

  final int businessId;
  final int userId;

  @override
  State<StatefulWidget> createState() => DashboardPageState();
}

class DashboardPageState extends State<DashboardPage> {
  List<Widget> cards = [];

  Set<Marker> markers = {};

  Widget mapWidget = CircularProgressIndicator();

  final api = GoogleGeocodingApi(googleApiKey, isLogged: false);

  @override
  void initState() {
    cards = [];
    Business? business = isar.business.get(100);

    api
        .search(business!.addressComplete!, language: 'de')
        .then((GoogleGeocodingResponse response) {
      MarkerGenerator(64)
          .createBitmapDescriptorFromIconData(
        Icons.home,
        Colors.white,
        const Color(0xFF60845E),
        const Color(0xff87A34E),
      )
          .then((
        BitmapDescriptor bitMapDescriptor,
      ) {
        final prettyAddress = response.results.firstOrNull?.mapToPretty();
        setState(() {
          markers.add(
            Marker(
              markerId: MarkerId(business.name!),
              position: LatLng(
                prettyAddress?.latitude ?? 0,
                prettyAddress?.longitude ?? 0,
              ),
              icon: bitMapDescriptor,
            ),
          );
          mapWidget = MapWidget(
            markers: markers,
            polygons: {},
            polylines: {},
          );
        });
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final AmbitoTheme theme = getTheme(context);

    cards = [];

    cards.add(
      _buildCard(
        context,
        Colors.green,
        'images/measure/nist_brut_unterschlupforte.jpg',
        'Meine\nMaßnahmen',
        '/dashboard/meine-massnahmen',
      ),
    );
    cards.add(
      _buildCard(
        context,
        Colors.green,
        'images/measure/steinhaufen.jpg',
        'Urkunde',
        '/dashboard/urkunde',
      ),
    );
    cards.add(
      _buildCard(
        context,
        Colors.green,
        'images/measure/staudenbeete.jpg',
        'Flächen',
        '/dashboard/flaechen',
      ),
    );
    cards.add(
      _buildCard(
        context,
        Colors.green,
        'images/measure/pflanzgefaesse.jpg',
        'Stammdaten',
        '/dashboard/stammdaten',
      ),
    );

    return BasePage().getPage(
      context,
      SingleChildScrollView(
        child: Align(
          alignment: Alignment.topCenter,
          child: Padding(
            padding: context.breakpoint.padding,
            child: SizedBox(
              width: 1152,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  theme.verticalSpacerMax,
                  Text(
                    'Willkommen, Max Mustermann!',
                    textAlign: TextAlign.start,
                    style: theme.headlineLarge.copyWith(
                      color: theme.currentColorScheme.onSurface,
                    ),
                  ),
                  theme.verticalSpacerMax,
                  Align(
                    alignment: Alignment.center,
                    child: Wrap(
                      alignment: WrapAlignment.center,
                      spacing: 32,
                      children: cards,
                    ),
                  ),
                  theme.verticalSpacerMax,
                  _buildContent(context, theme),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildContent(BuildContext context, AmbitoTheme theme) {
    return (context.breakpoint.breakpoint == myBreakpoints.sm!.breakpoint)
        ? _buildSmallContent(theme)
        : _buildNormalContent(theme);
  }

  Widget _buildCard(BuildContext context, Color background, String image,
      String title, String link) {
    return (context.breakpoint.breakpoint == myBreakpoints.sm!.breakpoint)
        ? _buildSmallCard(background, image, title, link)
        : _buildNormalCard(background, image, title, link);
  }
}
