import 'package:auto_size_text/auto_size_text.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:farm_tpf/custom_model/ActionType.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
import 'package:farm_tpf/presentation/screens/actions/environment_update/sc_edit_action_environment_update.dart';
import 'package:farm_tpf/presentation/screens/actions/sc_action.dart';
import 'package:farm_tpf/presentation/screens/plot_detail/bloc/cubit/plot_action_type_cubit.dart';
import 'package:farm_tpf/utils/const_assets.dart';
import 'package:farm_tpf/utils/const_color.dart';
import 'package:farm_tpf/utils/const_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get/get.dart';

class PlotActionScreen extends StatefulWidget {
  final int? cropId;
  final String? cropCode;
  final int? cropType;
  PlotActionScreen({
    this.cropId,
    this.cropCode,
    this.cropType,
  });
  @override
  _PlotActionScreenState createState() => _PlotActionScreenState();
}

class _PlotActionScreenState extends State<PlotActionScreen> with AutomaticKeepAliveClientMixin {
  List<ActionType> actions = <ActionType>[];

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

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

  Widget _createActionButtons(ActionType actionType, BuildContext _context) {
    return GestureDetector(
        onTap: () {
          if (actionType.name == 'ACTIVE_TYPE_UPDATE_ENV') {
            Get.to(EditActionEnvironmentUpdate(
              cropId: widget.cropId ?? -1,
              activityId: -1,
              isEdit: false,
            ));
          } else {
            Get.to(ActionScreen(
              cropId: widget.cropId ?? -1,
              idAction: actionType.id ?? -1,
              title: actionType.description ?? '',
              activityType: actionType.name ?? '',
              activityId: -1,
              isEdit: false,
            ));
          }
        },
        child: Container(
          margin: EdgeInsets.all(8),
          padding: EdgeInsets.all(4),
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey, width: 0.1),
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(8.0)),
            boxShadow: <BoxShadow>[
              BoxShadow(color: AppColors.GRAY1.withOpacity(0.2), offset: Offset(1.1, 1.1), blurRadius: 4.0),
            ],
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                flex: 1,
                child: SizedBox(
                  width: Get.width / 10,
                  height: Get.height / 10,
                  child: CachedNetworkImage(
                    imageUrl: '${ConstCommon.baseImageUrl}${actionType.urlLogo}',
                    placeholder: (context, url) => Icon(
                      Icons.broken_image,
                      color: Colors.grey[300],
                    ),
                    errorWidget: (context, url, error) => Image.asset(
                      AppAssets.logo,
                    ),
                  ),
                ),
              ),
              Expanded(
                flex: 1,
                child: Align(
                  alignment: Alignment.center,
                  child: AutoSizeText(
                    actionType.description ?? '',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 13,
                      color: AppColors.BLACK2,
                    ),
                    maxLines: 2,
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (contex) => PlotActionTypeCubit(Repository())
          ..getAllActionTypes(
            widget.cropType ?? -1,
          ),
        child: BlocBuilder<PlotActionTypeCubit, PlotActionTypeState>(
          builder: (context, state) {
            if (state is PlotActionTypeLoading) {
              return LoadingListPage();
            } else if (state is PlotActionTypeSuccess) {
              return GridView.count(
                  shrinkWrap: true,
                  crossAxisCount: 3,
                  children: state.actionTypes.map(
                    (item) {
                      return _createActionButtons(item, context);
                    },
                  ).toList());
            } else if (state is PlotActionTypeError) {
              return Center(child: Text(state.error));
            } else {
              //init
              return Container();
            }
          },
        ));
  }

  @override
  bool get wantKeepAlive => true;
}
