// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import 'fix_processor.dart';

void main() {
  defineReflectiveSuite(() {
    defineReflectiveTests(RemoveUnusedParameterTest);
    defineReflectiveTests(RemoveUnusedParameterBulkTest);
    defineReflectiveTests(RemoveUnusedParameterTestHint);
    defineReflectiveTests(RemoveUnusedParameterBulkTestHint);
  });
}

@reflectiveTest
class RemoveUnusedParameterBulkTest extends BulkFixProcessorTest {
  @override
  String get lintCode => LintNames.avoid_unused_constructor_parameters;

  Future<void> test_singleFile() async {
    await resolveTestCode('''
class C {
  int y;
  C({int x = 0, this.y = 0, int z = 0});
}
''');
    await assertHasFix('''
class C {
  int y;
  C({this.y = 0});
}
''');
  }
}

@reflectiveTest
class RemoveUnusedParameterBulkTestHint extends BulkFixProcessorTest {
  Future<void> test_singleFile() async {
    await resolveTestCode('''
class _C {
  int? a;
  int b;
  int? c;
  _C([this.a, this.b = 1, this.c]);
}

class _C2 {
  int? a;
  int b;
  int? c;
  _C2({this.a, this.b = 1, this.c});
}

void main() {
  print(_C());
  print(_C2());
}
''');
    await assertHasFix('''
class _C {
  int? a;
  int b;
  int? c;
  _C([this.b = 1]) : a = null : c = null;
}

class _C2 {
  int? a;
  int b;
  int? c;
  _C2({this.b = 1}) : a = null : c = null;
}

void main() {
  print(_C());
  print(_C2());
}
''');
  }
}

@reflectiveTest
class RemoveUnusedParameterTest extends FixProcessorLintTest {
  @override
  FixKind get kind => DartFixKind.removeUnusedParameter;

  @override
  String get lintCode => LintNames.avoid_unused_constructor_parameters;

  Future<void> test_fieldFormalParameter_withDefaultValue() async {
    await resolveTestCode('''
class _C {
  final int? _i;
  _C({this._i = 3});
}
''');
    await assertHasFix(
      '''
class _C {
  final int? _i;
  _C() : _i = 3;
}
''',
      filter: (diagnostic) =>
          diagnostic.diagnosticCode == diag.unusedElementParameter,
    );
  }

  Future<void>
  test_fieldFormalParameter_withDefaultValue_withInitializer() async {
    await resolveTestCode('''
class _C {
  final int? _i;
  final int _j;
  _C({this._i = 3}) : _j = 2;
}
''');
    await assertHasFix(
      '''
class _C {
  final int? _i;
  final int _j;
  _C() : _i = 3, _j = 2;
}
''',
      filter: (diagnostic) =>
          diagnostic.diagnosticCode == diag.unusedElementParameter,
    );
  }

  Future<void> test_fieldFormalParameter_withoutDefaultValue() async {
    await resolveTestCode('''
class _C {
  final int? _i;
  _C({this._i});
}
''');
    await assertHasFix(
      '''
class _C {
  final int? _i;
  _C() : _i = null;
}
''',
      filter: (diagnostic) =>
          diagnostic.diagnosticCode == diag.unusedElementParameter,
    );
  }

  Future<void>
  test_fieldFormalParameter_withoutDefaultValue_withInitializer() async {
    await resolveTestCode('''
class _C {
  final int? _i;
  final int _j;
  _C({this._i}) : _j = 2;
}
''');
    await assertHasFix(
      '''
class _C {
  final int? _i;
  final int _j;
  _C() : _i = null, _j = 2;
}
''',
      filter: (diagnostic) =>
          diagnostic.diagnosticCode == diag.unusedElementParameter,
    );
  }

  Future<void> test_first_optionalNamed_second_optionalNamed() async {
    await resolveTestCode('''
class C {
  int y;
  C({int x = 0, this.y = 0});
}
''');
    await assertHasFix('''
class C {
  int y;
  C({this.y = 0});
}
''');
  }

  Future<void> test_first_optionalPositional_second_optionalPositional() async {
    await resolveTestCode('''
class C {
  int y;
  C([int x = 0, this.y = 0]);
}
''');
    await assertHasFix('''
class C {
  int y;
  C([this.y = 0]);
}
''');
  }

  Future<void> test_first_required_second_optionalInvalid() async {
    await resolveTestCode('''
class C {
  C(int a, int b = 1,);
}
''');
    await assertHasFix(
      '''
class C {
  C(int b = 1,);
}
''',
      filter: (e) => e.offset == testCode.indexOf('int a'),
      allowFixAllFixes: true,
    );
  }

  Future<void> test_first_requiredPositional_second_optionalNamed() async {
    await resolveTestCode('''
class C {
  int y;
  C(int x, {this.y = 0});
}
''');
    await assertHasFix('''
class C {
  int y;
  C({this.y = 0});
}
''');
  }

  Future<void> test_first_requiredPositional_second_optionalPositional() async {
    await resolveTestCode('''
class C {
  int y;
  C(int x, [this.y = 0]);
}
''');
    await assertHasFix('''
class C {
  int y;
  C([this.y = 0]);
}
''');
  }

  Future<void> test_first_requiredPositional_second_requiredPositional() async {
    await resolveTestCode('''
class C {
  int y;
  C(int x, this.y);
}
''');
    await assertHasFix('''
class C {
  int y;
  C(this.y);
}
''');
  }

  Future<void> test_last_optionalNamed_noDefaultValue() async {
    await resolveTestCode('''
class C {
  C({int? x});
}
''');
    await assertHasFix('''
class C {
  C();
}
''');
  }

  Future<void> test_last_optionalNamed_previous_optionalNamed() async {
    await resolveTestCode('''
class C {
  int x;
  C({this.x = 0, int y = 0});
}
''');
    await assertHasFix('''
class C {
  int x;
  C({this.x = 0});
}
''');
  }

  Future<void> test_last_optionalNamed_previous_requiredPositional() async {
    await resolveTestCode('''
class C {
  int x;
  C(this.x, {int y = 0});
}
''');
    await assertHasFix('''
class C {
  int x;
  C(this.x);
}
''');
  }

  Future<void> test_last_optionalPositional_noDefaultValue() async {
    await resolveTestCode('''
class C {
  C([int? x]);
}
''');
    await assertHasFix('''
class C {
  C();
}
''');
  }

  Future<void>
  test_last_optionalPositional_previous_optionalPositional() async {
    await resolveTestCode('''
class C {
  int x;
  C([this.x = 0, int y = 0]);
}
''');
    await assertHasFix('''
class C {
  int x;
  C([this.x = 0]);
}
''');
  }

  Future<void>
  test_last_optionalPositional_previous_requiredPositional() async {
    await resolveTestCode('''
class C {
  int x;
  C(this.x, [int y = 0]);
}
''');
    await assertHasFix('''
class C {
  int x;
  C(this.x);
}
''');
  }

  Future<void>
  test_last_requiredPositional_previous_requiredPositional() async {
    await resolveTestCode('''
class C {
  int x;
  C(this.x, int y);
}
''');
    await assertHasFix('''
class C {
  int x;
  C(this.x);
}
''');
  }

  Future<void> test_only_optionalNamed() async {
    await resolveTestCode('''
class C {
  C({int x = 0});
}
''');
    await assertHasFix('''
class C {
  C();
}
''');
  }

  Future<void> test_only_optionalPositional() async {
    await resolveTestCode('''
class C {
  C([int x = 0]);
}
''');
    await assertHasFix('''
class C {
  C();
}
''');
  }

  Future<void> test_only_requiredPositional() async {
    await resolveTestCode('''
class C {
  C(int x);
}
''');
    await assertHasFix('''
class C {
  C();
}
''');
  }

  Future<void> test_primaryConstructor_named_optionalNamed() async {
    await resolveTestCode('''
class C.named({int? x});
''');
    await assertHasFix('''
class C.named();
''');
  }

  Future<void> test_primaryConstructor_unnamed_multiple() async {
    await resolveTestCode('''
class C(int x, var int y);
''');
    await assertHasFix('''
class C(var int y);
''');
  }

  Future<void> test_primaryConstructor_unnamed_requiredPositional() async {
    await resolveTestCode('''
class C(int x);
''');
    await assertHasFix('''
class C();
''');
  }
}

/// Situations exist where unused parameters are flagged by hints, rather
/// than lints.   Apply the same algorithm for parameter removal as for
/// lints.
@reflectiveTest
class RemoveUnusedParameterTestHint extends FixProcessorTest {
  @override
  FixKind get kind => DartFixKind.removeUnusedParameter;

  Future<void> test_optionalNamed() async {
    await resolveTestCode('''
class _C {
  final int variable;
  int? somethingElse;
  _C({required this.variable, this.somethingElse});
}

void main() {
  print(_C(variable: 123));
}
''');
    await assertHasFix('''
class _C {
  final int variable;
  int? somethingElse;
  _C({required this.variable}) : somethingElse = null;
}

void main() {
  print(_C(variable: 123));
}
''');
  }

  Future<void> test_optionalPositional() async {
    await resolveTestCode('''
class _C {
  int? somethingElse;
  int? entirely;
  _C([this.somethingElse, this.entirely]);
}

void main() {
  print(_C(123));
}
''');
    await assertHasFix('''
class _C {
  int? somethingElse;
  int? entirely;
  _C([this.somethingElse]) : entirely = null;
}

void main() {
  print(_C(123));
}
''');
  }

  Future<void> test_optionalSuperNamed() async {
    await resolveTestCode('''
class B {
  final int? key;
  B({this.key});
}

class _C extends B {
  final int variable;
  _C({super.key, required this.variable});
}

void main() {
  print(_C(variable: 123));
}
''');
    await assertHasFix('''
class B {
  final int? key;
  B({this.key});
}

class _C extends B {
  final int variable;
  _C({required this.variable});
}

void main() {
  print(_C(variable: 123));
}
''');
  }
}
