import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:injectable/injectable.dart';
import 'package:keyboard/base/base_controller.dart';
import 'package:keyboard/module/about/about_page.dart';
import 'package:keyboard/module/feedback/feedback_page.dart';
import 'package:keyboard/module/store/discount/discount_view.dart';

import '../../data/consts/error_code.dart';
import '../../data/repository/account_repository.dart';
import '../../plugins/keyboard_android_platform.dart';
import '../../resource/colors.gen.dart';
import '../profile/profile_page.dart';
import '../store/discount/discount_controller.dart';
import '../store/suprise/surprise_dialog.dart';
import '../../resource/string.gen.dart';
import '../../utils/http_handler.dart';
import '../../utils/toast_util.dart';
import '../store/store_page.dart';
import '../store/ticket/discount_ticket_dialog.dart';

@injectable
class MineController extends BaseController {
  final AccountRepository accountRepository;

  MineController(this.accountRepository);

  bool get isLogin => accountRepository.isLogin.value;

  String? get phone => accountRepository.loginPhoneNum.value;

  String getUserName() {
    if (isLogin || phone != null && phone!.length > 4) {
      return '${StringName.mineAccountLoggedDesc}${phone!.substring(phone!.length - 4)}';
    } else {
      return StringName.mineAccountNoLogin;
    }
  }

  clickVip() {
    // debugPrint('clickVip');
    // StorePage.start();
    KeyboardAndroidPlatform.getInstance().enableFloatingWindow(true);
    KeyboardAndroidPlatform.getInstance().openInputMethodSettings();
  }

  clickOnlineCustomerService() {
    debugPrint('clickOnlineCustomerService');
    accountRepository
        .loginUserLogin("11223344551", "1122")
        .then((data) {
          Get.back();
          ToastUtil.show(StringName.loginSuccess);
        })
        .catchError((error) {
          if (error is LoginTooOftenException) {
            ToastUtil.show(StringName.loginTooOftenToast);
            return;
          }
          if (error is ServerErrorException) {
            if (error.code == ErrorCode.verificationCodeError) {
              ToastUtil.show(StringName.loginVerificationCodeErrorToast);
            } else {
              ToastUtil.show(error.message);
            }
          } else {
            ToastUtil.show(StringName.loginFailedToast);
          }
        });
  }

  clickTutorials() {
    debugPrint('clickTutorials');
  }

  clickPersonalProfile() {
    debugPrint('clickPersonalProfile');
    // SurpriseDialog.show(clickConfirm: StorePage.start);
    // DiscountTicketDialog.show();
    // SmartDialog.show(
    //
    //   maskColor: ColorName.black70,
    //   backType: SmartBackType.block,
    //   clickMaskDismiss: true,
    //   alignment: Alignment.bottomCenter,
    //   keepSingle: true,
    //   tag: "discountDialog",
    //   onDismiss: () => Get.delete<DiscountController>(),
    //
    //   builder: (_) => DiscountView(),
    // );
    ProfilePage.start();

  }

  clickFeedback() {
    debugPrint('clickFeedback');

    FeedbackPage.start();
  }

  clickAboutUs() {
    debugPrint('clickAboutUs');
    AboutPage.start();
  }
}
