import 'package:farm_tpf/presentation/screens/actions/crop_status/sc_edit_action_crop_status.dart';
import 'package:farm_tpf/presentation/screens/actions/disease/sc_edit_action_disease.dart';
import 'package:farm_tpf/presentation/screens/actions/dung/sc_edit_action_dung.dart';
import 'package:farm_tpf/presentation/screens/actions/end/sc_edit_action_end.dart';
import 'package:farm_tpf/presentation/screens/actions/environment_update/sc_edit_action_environment_update.dart';
import 'package:farm_tpf/presentation/screens/actions/harvest/sc_edit_action_harvest.dart';
import 'package:farm_tpf/presentation/screens/actions/nursery/sc_edit_action_nursery.dart';
import 'package:farm_tpf/presentation/screens/actions/other/sc_edit_action_other.dart';
import 'package:farm_tpf/presentation/screens/actions/plant/sc_edit_action_plant.dart';
import 'package:farm_tpf/presentation/screens/actions/spraying/sc_edit_action_spraying.dart';
import 'package:farm_tpf/presentation/screens/actions/use_water/sc_edit_action_user_water.dart';
import 'package:farm_tpf/utils/const_assets.dart';
import 'package:farm_tpf/utils/const_color.dart';
import 'package:farm_tpf/utils/const_string.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class PlotActionScreen extends StatefulWidget {
  int? cropId;
  String? cropCode;
  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();
    _initActionButtons();
  }

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

  _initActionButtons() {
    //type: 0- Trồng, 1- ướm
    if (widget.cropType == 1) {
      actions.add(ActionType(
          plot_action_nursery,
          EditActionNurseryScreen(
            cropId: widget.cropId ?? -1,
          ),
          AppAssets.icActionNursery));
    } else if (widget.cropType == 0) {
      actions.add(ActionType(
          plot_action_plant,
          EditActionPlantScreen(
            cropId: widget.cropId ?? -1,
          ),
          AppAssets.icActionPlant));
    }

    actions.add(ActionType(
        plot_action_crop_status,
        EditActionCropStatusScreen(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionCropStatus));
    actions.add(ActionType(
        plot_action_environment_update,
        EditActionEnvironmentUpdate(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionEnvironment));
    actions.add(ActionType(
        plot_action_dung,
        EditActionDungScreen(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionDung));
    actions.add(ActionType(
        plot_action_spraying,
        EditActionSprayingScreen(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionSpraying));
    actions.add(ActionType(
        plot_action_disease,
        EditActionDiseaseScreen(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionDisease));
    actions.add(ActionType(
        plot_action_use_water,
        EditActionUseWaterScreen(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionUseWater));
    actions.add(ActionType(
        plot_action_other,
        EditActionOtherScreen(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionOther));
    actions.add(ActionType(
        plot_action_harvest,
        EditActionHarvestScreen(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionHarvest));
    actions.add(ActionType(
        plot_action_finish,
        EditActionEndScreen(
          cropId: widget.cropId ?? -1,
        ),
        AppAssets.icActionEnd));
  }

  Widget _createActionButtons(ActionType actionType, BuildContext _context) {
    return GestureDetector(
        onTap: () {
          Navigator.of(context).push(MaterialPageRoute(builder: (context) => actionType.listScreen));
        },
        child: Container(
          margin: const EdgeInsets.all(8),
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey, width: 0.1),
            color: Colors.white,
            borderRadius: const BorderRadius.all(Radius.circular(8.0)),
            boxShadow: <BoxShadow>[
              BoxShadow(color: AppColors.GRAY1.withOpacity(0.2), offset: const Offset(1.1, 1.1), blurRadius: 4.0),
            ],
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Image.asset(
                actionType.icon,
                width: Get.width / 9,
                height: Get.width / 9,
              ),
              Text(
                actionType.actionName,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 13,
                  color: AppColors.BLACK2,
                ),
              ),
            ],
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return GridView.count(
        shrinkWrap: true,
        crossAxisCount: 3,
        children: actions.map(
          (item) {
            return _createActionButtons(item, context);
          },
        ).toList());
  }

  @override
  bool get wantKeepAlive => true;
}

class ActionType {
  Widget listScreen = SizedBox();
  String actionName = '';
  String icon = '';
  ActionType(String actionName, Widget listScreen, String icon) {
    this.actionName = actionName;
    this.listScreen = listScreen;
    this.icon = icon;
  }
}
