part of '../pages.dart';

class ButtonsPageDynamic extends StatefulWidget {
  static const routeName = '/buttons_page/buttons_page_dynamic';

  @override
  _ButtonsPageDynamicState createState() => _ButtonsPageDynamicState();
}

class _ButtonsPageDynamicState extends State<ButtonsPageDynamic> {
  @override
  void initState() {
    super.initState();
  }

  final pageJson = '''
  {
    "widget": "Scaffold",
    "appBar": {
      "widget": "AppBar",
      "centerTitle": true,
      "backgroundColor": "lls_primary",
      "title": {
        "widget": "Text",
        "data": "Buttons (dynamic)",
        "style": {
          "fontSize": 17.0
        }
      },
      "leading": {
        "widget": "InkWell",
        "onTap": {
          "function": "goToBack"
        },
        "child": {
          "widget": "Container",
          "child": {
            "widget": "Icon",
            "data": "ios.back",
            "color": "lls_white"
          }
        }
      }
    },
    "body": {
      "widget": "ListView",
      "children": [
        {
          "widget": "SizedBox",
          "height": 20.0
        },
        {
          "widget": "GestureDetector",
          "onTap": {
            "function": "goToStandardButtonPage"
          },
          "child": {
            "widget": "Container",
            "margin": "15,20,15,0",
            "padding": "5,5,5,5",
            "decoration": {
              "borderRadius": {
                "borderRadiusMethod": "circular",
                "radius": [7.0]
              },
              "color": "lls_secondary",
              "boxShadow": [
                {
                  "color": "lls_dark,withOpacity,0.40",
                  "blurRadius": 5.0
                }
              ]
            },
            "child": {
              "widget": "LLSListTile",
              "color": "lls_secondary",
              "title": {
                "widget": "Text",
                "data": "Standards Buttons",
                "style": {
                  "color": "lls_white"
                }
              },
              "icon": {
                "widget": "Icon",
                "data": "ios.forward",
                "color": "lls_white"
              }
            }
          }
        }
      ]
    }
  }
  ''';

  // final pageJson = '''
  // {
  //   "widget": "Scaffold",
  //   "body": {
  //     "widget": "Container",
  //     "margin": "15,20,15,0",
  //     "padding": "5,5,5,5",
  //     "decoration": {
  //       "borderRadius": {
  //         "borderRadiusMethod": "circular",
  //         "radius": [
  //           7.0
  //         ]
  //       },
  //       "color": "lls_secondary",
  //       "boxShadow": [
  //         {
  //           "color": "lls_dark,withOpacity,0.40",
  //           "blurRadius": 5.0
  //         }
  //       ]
  //     },
  //     "child": {
  //       "widget": "LLSListTile",
  //       "color": "lls_secondary",
  //       "title": {
  //         "widget": "Text",
  //         "data": "Standards Buttons",
  //         "style": {
  //           "color": "lls_white"
  //         }
  //       },
  //       "icon": {
  //         "widget": "Icon",
  //         "data": "ios.forward",
  //         "color": "lls_white"
  //       }
  //     }
  //   }
  // }
  // ''';

  Future<Widget?> _buildWidget(BuildContext context) async {
    try {
      return WidgetParserBuilder.build(pageJson, context, ButtonsPageMethodListener(), null);
    } catch (e) {
      String err = e.toString();
      return _buildErrorPage("${err.split('.')[0]} ${err.split('.')[1]}");
    }
  }

  Widget _buildErrorPage(String error) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                error,  
                style: TextStyle(fontSize: 14),
              ),
            ),
            const SizedBox(
              height: 15
            ),
            LLSButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text(
                'Back',
                style: TextStyle(fontSize: 16),
              ),
              type: LLSButtonType.outline,
              color: LLSColors.SUCCESS,
            ),
          ]
        ),
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Widget?>(
      future: _buildWidget(context),
      builder: (BuildContext context, AsyncSnapshot<Widget?> snapshot) {
        if (snapshot.hasError) {
          return _buildErrorPage(snapshot.error.toString());
        }
        return snapshot.hasData ? snapshot.data! : Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                CircularProgressIndicator()
              ]
            ),
          )
        );
      },
    );
  }
}

class ButtonsPageMethodListener extends MethodListener {
  @override
  run(BuildContext context, Map<String, dynamic>? event) {
    if (isFunctionNameExist(event)) {
      if (event!['function'] == 'goToStandardButtonPage') {
        Navigator.of(context).pushNamed(
          StandardsButtonPage.routeName
        );
        // if (isParametersExist(event)) {
        //   printWrapped(event['parameters']['tapDownDetails']);
        // }
      } else if (event['function'] == 'goToBack') {
        Navigator.of(context).pop();
      }
    } else {
      print("receiver event: ${event.toString()}");
    }
  }
}