import 'package:chat/data/UserData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/home/DiscoverPage.dart';
import 'package:chat/home/ParkPage.dart';
import 'package:chat/home/follow_user_dynamic.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:flutter/material.dart';
import 'package:location_permissions/location_permissions.dart';
import 'package:oktoast/oktoast.dart';

class FindPage extends StatefulWidget {
  FindPage({Key key}) : super(key: key);

  @override
  _FindPageState createState() => new _FindPageState();
}

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

  @override
  Widget build(BuildContext context) {
    Widget appBar = new AppBar(
      backgroundColor: AppColors.NewAppbarBgColor,
      title: new Text(I18n.of(context).find,
          textScaleFactor: 1.0, style: Constants.MainTitleStyle),
      centerTitle: false,
      elevation: 1,
    );
    return SafeArea(
        child: Scaffold(
      appBar: appBar,
      body: _buildBody(),
    ));
  }

  _buildBody() {
    return Column(
      children: <Widget>[
        _buildRadioButton(),
        _buildMyProgramButton(),
        _buildNearButton(),
        _buildScanButton(),
      ],
    );
  }

  _buildIconButton(Widget icon, Gradient gradient, String text, callback,
      {double marginTop: 12, bool showDivder: false}) {
    return InkWell(
        onTap: callback,
        child: Container(
            margin: EdgeInsets.only(top: marginTop),
            padding: EdgeInsets.only(left: 16.5, right: 20, top: 9, bottom: 9),
            decoration: BoxDecoration(
                color: Colors.white,
                border:
                    showDivder ? Border(top: Constants.GreyBorderSide) : null),
            child: Row(
              children: <Widget>[
                Stack(
                  alignment: Alignment.center,
                  children: <Widget>[
                    Container(
                      width: Constants.ContactAvatarSize,
                      height: Constants.ContactAvatarSize,
                      decoration: BoxDecoration(
                          gradient: gradient,
                          borderRadius: BorderRadius.circular(4)),
                    ),
                    icon
                  ],
                ),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 15),
                    child: Text(
                      text,
                      textScaleFactor: 1.0,
                      style: TextStyle(fontSize: 16),
                    ),
                  ),
                ),
                Icon(
                  IconData(0xe63c, fontFamily: 'iconfont'),
                  size: 25.0,
                  color: Color(AppColors.TabIconNormal),
                )
              ],
            )));
  }

  _buildRadioButton() {
    Widget icon = Positioned(
        // left: 4.5,
        top: 5,
        child: Icon(
          IconData(0xe66b, fontFamily: Constants.IconFontFamily),
          color: Colors.white,
        ));

    var gradient = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: <Color>[
          const Color(0xFFFBD25C),
          const Color(0xFFF57723),
        ]);

    return _buildIconButton(icon, gradient, I18n.of(context).radio, () {
      if (!UserData().stationOpenSwitch) {
        showToast(I18n.of(context).system_not_open);
        return;
      }
      if (!UserData().hasLocationPermission) {
        CustomUI.buildOneConfirm(
            context, I18n.of(context).get_location, I18n.of(context).determine,
            () {
          Navigator.of(context).pop();
          LocationPermissions().openAppSettings();
        });
        return;
      }
      Navigator.of(context).push(
        new MaterialPageRoute(
          builder: (context) {
            return ParkPage();
          },
        ),
      );
    });
  }

  _buildMyProgramButton() {
    Widget icon = Icon(
      IconData(0xe66d, fontFamily: Constants.IconFontFamily),
      color: Colors.white,
    );

    var gradient = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: <Color>[
          const Color(0xFFF87354),
          const Color(0xFFF23D6B),
        ]);

    return _buildIconButton(
        icon,
        gradient,
        UserData().isMan()
            ? I18n.of(context).woman_gold
            : I18n.of(context).man_gold, () {
      if (!UserData().myProgramOpenSwitch) {
        showToast(I18n.of(context).system_not_open);
        return;
      }
      Navigator.of(context).push(
        new MaterialPageRoute(
          builder: (context) {
            return FollowUserPage();
          },
        ),
      );
    }, marginTop: 0, showDivder: true);
  }

  _buildNearButton() {
    Widget icon = Icon(
      IconData(0xe665, fontFamily: Constants.IconFontFamily),
      color: Colors.white,
    );

    var gradient = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: <Color>[
          const Color(0xFFB678FF),
          const Color(0xFF6760FC),
        ]);

    return _buildIconButton(icon, gradient, I18n.of(context).nearby, () {
      if (!UserData().nearbyOpenSwitch) {
        showToast(I18n.of(context).system_not_open);
        return;
      }

      if (!UserData().hasLocationPermission) {
        CustomUI.buildOneConfirm(
            context, I18n.of(context).get_location, I18n.of(context).determine,
            () {
          Navigator.of(context).pop();
          LocationPermissions().openAppSettings();
        });
        return;
      }

      Navigator.of(context).push(
        new MaterialPageRoute(
          builder: (context) {
            return DisCoverPage();
          },
        ),
      );
    });
  }

  _buildScanButton() {
    Widget icon = Positioned(
        top: 5,
        child: Icon(
          IconData(0xe666, fontFamily: Constants.IconFontFamily),
          color: Colors.white,
        ));

    var gradient = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: <Color>[
          const Color(0xFF48DFF2),
          const Color(0xFF0080FF),
        ]);

    return _buildIconButton(icon, gradient, I18n.of(context).scan, () {
      CustomUI().goScanPage(context);
    });
  }

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