def update_dart(new_version)
  path = Dir['../lib/**/qonversion_internal.dart'].first
  regex = /static const String sdkVersion = ".*";/
  result_value = "static const String sdkVersion = \"#{new_version}\";"

  update_file(path, regex, result_value)
end

def update_yaml(new_version)
  path = "../pubspec.yaml"
  regex = /version: .*/
  result_value = "version: #{new_version}"

  update_file(path, regex, result_value)
end

def update_file(path, regex, result_value)
  file = File.read(path)
  new_content = file.gsub(regex, result_value)
  File.open(path, 'w') { |line| line.puts new_content }
end

def update_changelog(new_version)
  changelog_update = "\#\# #{new_version}\n* // Update changelog here\n\n"
  path = "../CHANGELOG.md"
  file = File.read(path)
  File.open(path, 'w') { |line| 
    line.puts changelog_update + file 
  }
end

def upgrade_sandwich_android(new_version)
  path = "../android/build.gradle"
  common_part = "implementation \"io.qonversion:sandwich:"
  regex = /#{common_part}.*"/
  result_value = "#{common_part}#{new_version}\""

  update_file(path, regex, result_value)
end

def upgrade_sandwich_apple(platform, new_version)
  path = "../#{platform}/qonversion_flutter.podspec"
  common_part = "s.dependency \"QonversionSandwich\", \""
  regex = /#{common_part}.*"/
  result_value = "#{common_part}#{new_version}\""

  update_file(path, regex, result_value)
end

def update_file(path, regex, result_value)
  file = File.read(path)
  new_content = file.gsub(regex, result_value)
  File.open(path, 'w') { |line| line.puts new_content }
end

def get_tag
  tag = last_git_tag()
  puts tag
  result_tag = tag.scan(%r{\d{1,2}.\d{1,2}.\d{1,3}}).first
  return result_tag
end

def calculate_minor_version(tag)
  major, minor, patch = parse_versions(tag)
  new_minor_version = minor.to_i.next.to_s
  new_version = major + "." + new_minor_version + "." + "0"
  return new_version
end

def calculate_patch_version(tag)
  major, minor, patch = parse_versions(tag)
  new_patch_version = patch.to_i.next.to_s
  new_version = major + "." + minor + "." + new_patch_version

  return new_version
end

def push_tag(tag)
  system("git checkout develop")
  system("git pull origin develop")
  add_git_tag(tag: tag)
  push_git_tags(tag: tag)
end

def parse_versions(tag)
  split_version_array = tag.split(".", 3)

  if split_version_array.length == 3
    major = split_version_array[0]
    minor = split_version_array[1]
    patch = split_version_array[2]

    return major, minor, patch
  end
end

lane :patch do
  tag = get_tag
  new_version = calculate_patch_version(tag)
  new_tag = "prerelease/" + new_version
  push_tag(new_tag)
end

lane :minor do
  tag = get_tag
  new_version = calculate_minor_version(tag)
  new_tag = "prerelease/" + new_version
  push_tag(new_tag)
end

lane :bump do |options|
  new_version = options[:version]

  update_dart(new_version)
  update_yaml(new_version)
  update_changelog(new_version)
end

lane :upgrade_sandwich do |options|
  new_version = options[:version]

  upgrade_sandwich_android(new_version)
  upgrade_sandwich_apple("ios", new_version)
  upgrade_sandwich_apple("macos", new_version)
end

lane :provide_next_patch_version do
  tag = get_tag
  new_version = calculate_patch_version(tag)
  sh("echo version=#{new_version} >> \"$GITHUB_ENV\"")
end
