import 'package:another_flushbar/flushbar.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:telnow_mobile_new/src/utils/U.dart';

class WidgetComponent {

  static Widget searchBar({String hintText = '', initialValue = '', required Function(dynamic) onSearch}){
    TextEditingController searchKey = TextEditingController();
    if(hintText == ''){
      hintText = 'searchAsk'.tr();
    }
    if(initialValue != ''){
      searchKey.text = initialValue;
    }

    return Padding(
      padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
      child: TextFormField(
        controller: searchKey,
        maxLines: 1,
        style: TextStyle(fontSize: 14, color: Colors.black),
        decoration: InputDecoration(
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(32),
            borderSide: BorderSide(width: 1.0, color: Colors.black45),
          ),
          prefixIcon: U.iconsax('search-normal-1', color: textColor),
          hintText: hintText,
          hintStyle: TextStyle(fontSize: 14, color: Colors.black54),
          contentPadding: EdgeInsets.only(right: 15),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(width: 1.0, color: Colors.black45),
            borderRadius: BorderRadius.circular(32),
          ),
          focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(width: 1.0, color: primaryColor),
              borderRadius: BorderRadius.circular(32)
          ),
        ),
        textInputAction: TextInputAction.done,
        onFieldSubmitted: onSearch,
      ),
    );
  }

  static Widget boxSeparator(){
    return Container(
        height: 12,
        width: double.infinity,
        decoration: BoxDecoration(
            color: Color(0xFFFAFAFA),
            border: Border(
                top: BorderSide(
                    color: Color(0xFFDCDCDC),
                    width: 1.0
                )
            )
        )
    );
  }

  static Widget buttonWithIcon(String label, IconData icon, VoidCallback onPressed){
    return TextButton.icon(
      style: TextButton.styleFrom(
        shape: const StadiumBorder(),
        backgroundColor: Color(0xFF00BFA6).withAlpha(15),
        foregroundColor: Color(0xFF00BFA6),
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 1),
        minimumSize: const Size(0, 36),
      ),
      onPressed: onPressed,
      icon: Icon(icon, color: Color(0xFF078C84), size: 16,),
      label: Text(
        label,
        style: const TextStyle(color: Color(0xFF078C84), fontSize: 14, fontWeight: FontWeight.w500),
      ),
    );
  }

  static Widget button({required String label, textColor = Colors.white, color = primaryColor, border = primaryColor, action}) {
    return SizedBox(
        width: double.infinity,
        height: 48,
        child: TextButton(
          style: ButtonStyle(
            backgroundColor: WidgetStatePropertyAll(color),
            shape: WidgetStatePropertyAll(
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(12), side: BorderSide(color: border))
            )),
          child: Text(label, style: TextStyle(color: textColor, fontSize: 16), textAlign: TextAlign.center),
          onPressed: () => action(),
        )
    );
  }

  static Widget bottomButtonGroup(
    {
      required VoidCallback actionSave,
      required VoidCallback actionCancel,
      required String submitLabel,
      required String cancelLabel,
      Color submitColor = primaryColor,
      Color submitBorder = primaryColor,
      EdgeInsets padding = const EdgeInsets.symmetric(horizontal: 15, vertical: 15)
    }){
    return Container(
      padding: padding,
      width: double.infinity,
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
              top: BorderSide(
                  color: Color(0xFFDCDCDC),
                  width: 1.0
              )
          )
      ),
      child: Row(
        children: [
          Expanded(
              child: button(
                label: cancelLabel,
                color: Colors.white,
                textColor: Color(0xffa5a5a5),
                border: Color(0xffa5a5a5),
                action: actionCancel,
              )
          ),
          SizedBox(width: 10),
          Expanded(
              child: button(
                color: submitColor,
                border: submitBorder,
                label: submitLabel,
                action: actionSave,
              )
          )
        ],
      ),
    );
  }

  static Widget lineSeparator(){
    return Padding(padding: const EdgeInsets.symmetric(horizontal: 0.0, vertical: 5.0),
        child: Divider(color: Color(0xffF5F5F5), thickness: 1.0,)
    );
  }

  static Widget fieldCustom(String unit, TextEditingController controller, callBack){
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Color(0xffDCDCDC), width: 1),
        borderRadius: BorderRadius.circular(5),
      ),
      height: 40,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: TextField(
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                hintText: '0',
                hintStyle: TextStyle(color: Color(0xFFA3A3A3)),
                border: InputBorder.none,
                contentPadding: EdgeInsets.only(left: 10, bottom: 10),
              ),
              controller: controller,
              onChanged: callBack,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(right: 8.0),
            child: Text(unit, style: TextStyle(color: Color(0xFFA3A3A3)),),
          ),
        ],
      ),
    );
  }

  static Widget deleteButton(VoidCallback onTap){
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: const Color(0xFFC9223B).withAlpha(20),
          borderRadius: BorderRadius.circular(8),
        ),
        child: const Icon(
          Icons.delete_sweep,
          color: Color(0xFFC9223B),
          size: 24,
        ),
      ),
    );
  }

  static Widget popUpConfirm(context, String title, String message){
    return AlertDialog(
      title: Text(title, style: TextStyle(fontSize: 15)),
      content: Text(message, style: TextStyle(fontSize: 15)),
      actions: <Widget>[
        TextButton(
          child: Text('cancel'.tr()),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        TextButton(
          style: ButtonStyle(
            foregroundColor: WidgetStateProperty.all<Color>(Colors.red),
          ),
          child: Text('confirm'.tr()),
          onPressed: () {
            Navigator.pop(context, {'remove': true});
          },
        ),
      ],
    );
  }

  static Widget loading(){
    return Row(
      children: [
        SizedBox(width: 12, height:12, child: CircularProgressIndicator(strokeWidth: 0.6,)),
        SizedBox(width: 10,),
        Text('loading'.tr()),
      ],
    );
  }

  static Future<dynamic> alert(message, isError, context){
    return Flushbar(
      message: message,
      icon: Icon(
        Icons.info_outline,
        size: 28.0,
        color: isError?Colors.red:Colors.green,
      ),
      duration: Duration(seconds: 5),
      flushbarPosition: FlushbarPosition.BOTTOM,
      margin: EdgeInsets.all(8),
      borderRadius: BorderRadius.circular(8),
    ).show(context);
  }
}