import 'package:flutter/cupertino.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/data/repository/account_repository.dart';
import 'package:get/get.dart';

import '../../resource/string.gen.dart';
import '../../utils/error_handler.dart';
import '../../utils/toast_util.dart';

enum FeedbackType {
  feedback(1),
  complaint(2);

  final int value;

  const FeedbackType(this.value);
}

@injectable
class FeedbackController extends BaseController {
  final RxString _phone = ''.obs;
  final RxString _content = "".obs;

  final Rx<FeedbackType> type = FeedbackType.feedback.obs;

  get phone => _phone.value;

  get content => _content.value;

  final AccountRepository accountRepository;

  FeedbackController(this.accountRepository);

  onPhoneChanged(String phone) {
    _phone.value = phone;
  }

  onContentChanged(String content) {

    _content.value = content;
  }

  backClick() {
    debugPrint("backClick");
    Get.back();
  }

  submitClick() {
    if (_content.value.isEmpty) {
      if (type.value == FeedbackType.feedback) {
        ToastUtil.show(StringName.feedbackContentEmpty);
      } else {
        ToastUtil.show(StringName.complaintContentEmpty);
      }

      return;
    }
    if (_phone.value.isEmpty) {
      ToastUtil.show(StringName.feedbackPhoneEmpty);
      return;
    }
    accountRepository
        .complaintSubmit(_phone.value, _content.value)
        .then((data) {
          if (type.value == FeedbackType.feedback) {
            ToastUtil.show(StringName.feedbackSubmitSuccess);
          } else {
            ToastUtil.show(StringName.complaintContentSuccess);
          }
          Get.back();
        })
        .catchError((error) {
          ErrorHandler.toastError(error);
          ToastUtil.show("提交失败，请稍后重试");
        });
  }

  @override
  void onInit() {
    super.onInit();
    final arguments = Get.arguments;
    if (arguments != null && arguments["type"] != null) {
      type.value = arguments["type"];
    }
  }

  getTitleText() {
    if (type.value == FeedbackType.feedback) {
      return StringName.feedback;
    } else {
      return StringName.complaintReport;
    }
  }
  getContentTitle(){
    if (type.value == FeedbackType.feedback) {
      return StringName.feedbackContentTitle;
    } else {
      return StringName.complaintContentTitle;
    }
  }

  getContentHint() {
    if (type.value == FeedbackType.feedback) {
      return StringName.feedbackContentHint;
    } else {
      return StringName.complaintContentHint;
    }
  }
}
