package torrent;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.Base64;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by denis on 02/10/2016.
 */
public class Torrent {

    public String name;
    public Date date;
    //public long size;
    public String size;
    public double lsize;
    public int seeder;
    public int leecher;
    public String magnetUri;
    public String category;
    public double searchRating = 0.0;
    public String status;
    public String progress;
    public String remoteId;
    public String folder_id;
    public String file_id;

    @Override
    public String toString() {
        DecimalFormat df = new DecimalFormat("#.##");
        double seedRatio;

        if (this.leecher > 0) {
            seedRatio = (double) this.seeder / (double) this.leecher;
        } else {
            seedRatio = this.seeder;
        }
        String magnetUriBase64 = "";
        if (magnetUri != null) {
            try {
                magnetUriBase64 = Base64.getUrlEncoder().encodeToString(magnetUri.getBytes("utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return "[" + this.name + "][" + this.size + "][" + this.leecher + "/" + this.seeder + "@" + df.format(seedRatio) + "] R:" + df.format(this.searchRating) + " <a href=\"./boat/download/?d=" + magnetUriBase64 + "\">Download</a> RID: " + this.remoteId + " Status/Progress:" + this.status + "/" + this.progress + "<br/>";
    }

    @Override
    public boolean equals(Object obj) {
        return this.getTorrentId().equals(((Torrent) obj).getTorrentId());
    }

    public String getTorrentId() {
        if (this.magnetUri == null) {
            return String.valueOf(this.hashCode());
        }
        Pattern magnetPattern = Pattern.compile("(btih:)([a-z0-9]*)(&dn)");
        Matcher matcher = magnetPattern.matcher(this.magnetUri);
        if (matcher.find()) {
            return matcher.group(2);
        } else {
            return String.valueOf(this.hashCode());
        }
    }
}

@@ -27,39 +27,36 @@
 ava.
+n
 io.
-UnsupportedEncodingException
+charset.StandardCharsets
 ;%0Aim
@@ -1031,30 +1031,8 @@
 ) %7B%0A
-            try %7B%0A    
     
@@ -1118,151 +1118,134 @@
 tes(
-%22utf-8%22));%0A            %7D catch (UnsupportedEncodingException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A%0A        return 
+StandardCharsets.UTF_8));%0A        %7D%0A%0A        StringBuilder stringBuilder = new StringBuilder();%0A%0A        stringBuilder.append(
 %22%5B%22 
@@ -1390,16 +1390,76 @@
 ng) + %22 
+%22);%0A%0A        // download link%0A        stringBuilder.append(%22
 %3Ca href=
@@ -1521,31 +1521,96 @@
 %3C/a%3E
+%22);%0A%0A        if (remoteId != null) %7B%0A            stringBuilder.append(%22
  RID:
- 
 %22 + 
-this.
 remoteId
  + %22
@@ -1609,33 +1609,114 @@
 teId
- + %22 Status/Progress:%22 + 
+);%0A        %7D%0A        if (this.status != null && this.progress != null) %7B%0A            stringBuilder.append(
 this
@@ -1722,25 +1722,27 @@
 s.status + %22
-/
+ / 
 %22 + this.pro
@@ -1750,18 +1750,101 @@
 ress
- + 
+);%0A        %7D%0A%0A        stringBuilder.append(
 %22%3C
+/
 br
-/
 %3E%22
+);%0A%0A        return stringBuilder.toString()
 ;%0A  

improves string representation