package org.jenkinsci.plugins.p4.credentials;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.Serializable;

public class TicketModeImpl extends AbstractDescribableImpl<TicketModeImpl> implements Serializable {

	private static final long serialVersionUID = 1L;

	@NonNull private final String value;

	@NonNull private final String ticketValue;

	@NonNull private final String ticketPath;

	@DataBoundConstructor
	public TicketModeImpl(String value, String ticketValue, String ticketPath) {
		this.value = value;
		this.ticketValue = (ticketValue != null) ? ticketValue : "";
		this.ticketPath = (ticketPath != null) ? ticketPath : "";
	}

	public String getValue() {
		return value;
	}

	public String getTicketValue() {
		return ticketValue;
	}

	public String getTicketPath() {
		return ticketPath;
	}

	public boolean isTicketValueSet() {
		return !StringUtils.isEmpty(getTicketValue());
	}

	public boolean isTicketPathSet() {
		return !StringUtils.isEmpty(getTicketPath());
	}

	@Extension
	@Symbol("ticketMode")
	public static class DescriptorImpl extends Descriptor<TicketModeImpl> {
		@Override
		public String getDisplayName() {
			return "TicketMode";
		}
	}
}

@@ -190,16 +190,43 @@
 riptor;%0A
+import hudson.util.Secret;%0A
 import o
@@ -525,33 +525,34 @@
 = 1L;%0A%0A%09@NonNull
- 
+%0A%09
 private final St
@@ -565,33 +565,84 @@
 alue;%0A%0A%09@NonNull
- 
+%0A%09private final Secret ticketSecret;%0A%0A%09@Deprecated%0A%09
 private final St
@@ -669,17 +669,18 @@
 @NonNull
- 
+%0A%09
 private 
@@ -853,32 +853,52 @@
 e = 
-(ticketValue != null) ? 
+%22%22;%0A%09%09this.ticketSecret = Secret.fromString(
 tick
@@ -904,21 +904,17 @@
 ketValue
- : %22%22
+)
 ;%0A%09%09this
@@ -1059,26 +1059,198 @@
 %7B%0A%09%09
-return ticketValue
+if (ticketSecret != null && !getTicketSecret().isEmpty()) %7B%0A%09%09%09return getTicketSecret();%0A%09%09%7D%0A%09%09return ticketValue;%0A%09%7D%0A%0A%09public String getTicketSecret() %7B%0A%09%09return ticketSecret.getPlainText()
 ;%0A%09%7D

Store Perforce Ticket as a Secret, but support old clear text ticket credentials.