part of 'pages.dart';

class TestStatePage extends StatefulWidget {
  static const routeName = '/test_state_page';

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

class _TestStatePageState extends State<TestStatePage> {
  @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"
              }
            }
          }
        }
      ]
    }
  }
  ''';

  Future<Widget?> _buildWidget(BuildContext context) async {
    try {
      return WidgetParserBuilder.build(pageJson, context, MethodListener(), null);
    } catch (e) {
      String err = e.toString();
      print(err);
      return _buildErrorPage("Page Rendering is Error. $err");
    }
  }

  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()
              ]
            ),
          )
        );
      },
    );
  }
}