package com.czbix.v2ex.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.czbix.v2ex.AppCtx;

import java.util.regex.Pattern;

public class Avatar implements Parcelable {
    public static final float DENSITY;
    public static final int SIZE_LARGE = 73;
    public static final int SIZE_NORMAL = 48;
    public static final int SIZE_MINI = 24;

    private static final Pattern PATTERN = Pattern.compile("(mini|normal|large)");
    private static final String LARGE = "large";
    private static final String NORMAL = "normal";
    private static final String MINI = "mini";

    private final String mBaseUrl;

    static {
        DENSITY = AppCtx.getInstance().getResources().getDisplayMetrics().density;
    }

    Avatar(String baseUrl) {
        mBaseUrl = baseUrl;
    }

    public String getLarge() {
        return getUrl(mBaseUrl, LARGE);
    }

    public String getNormal() {
        return getUrl(mBaseUrl, NORMAL);
    }

    public String getMini() {
        return getUrl(mBaseUrl, MINI);
    }

    public String getBaseUrl() {
        return mBaseUrl;
    }

    public String getUrlByDp(float dp) {
        final float size = dp * DENSITY;
        if (size >= SIZE_LARGE) {
            return getLarge();
        } else if (size >= SIZE_NORMAL) {
            return getNormal();
        } else {
            return getMini();
        }
    }

    public static String getUrl(String baseUrl, String size) {
        return "https:" + String.format(baseUrl, size);
    }

    private static String getBaseUrl(String url) {
        return PATTERN.matcher(url).replaceFirst("%s");
    }

    public static class Builder {
        private String mBaseUrl;

        public Builder setUrl(String url) {
            mBaseUrl = getBaseUrl(url);
            return this;
        }

        public Builder setBaseUrl(String url) {
            mBaseUrl = url;
            return this;
        }

        public Avatar createAvatar() {
            return new Avatar(mBaseUrl);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.mBaseUrl);
    }

    protected Avatar(Parcel in) {
        this.mBaseUrl = in.readString();
    }

    public static final Creator<Avatar> CREATOR = new Creator<Avatar>() {
        public Avatar createFromParcel(Parcel source) {
            return new Avatar(source);
        }

        public Avatar[] newArray(int size) {
            return new Avatar[size];
        }
    };
}

@@ -1175,24 +1175,107 @@
  * DENSITY;%0A
+        return getUrlByPx(size);%0A    %7D%0A%0A    public String getUrlByPx(float size) %7B%0A
         if (
@@ -1283,21 +1283,20 @@
 ize 
-%3E
+%3C
 = SIZE_
-LARGE
+MINI
 ) %7B%0A
@@ -1317,21 +1317,20 @@
 turn get
-Large
+Mini
 ();%0A    
@@ -1349,17 +1349,17 @@
 f (size 
-%3E
+%3C
 = SIZE_N
@@ -1430,36 +1430,37 @@
       return get
-Mini
+Large
 ();%0A        %7D%0A  

fix avatar size bug