package com.turlir.abakgists.model;

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import com.turlir.abakgists.allgists.view.TypesFactory;
import com.turlir.abakgists.allgists.view.ViewModel;

public class GistModel
        extends ViewModel
        implements Parcelable {

    public final String id;

    public final String url;

    public final String created;

    @NonNull
    public final String description;

    public final String ownerLogin;

    public final String ownerAvatarUrl;

    @NonNull
    public final String note;

    public final boolean isLocal;

    public static final Parcelable.Creator<GistModel> CREATOR = new Parcelable.Creator<GistModel>() {
        @Override
        public GistModel createFromParcel(Parcel in) {
            return new GistModel(in);
        }

        @Override
        public GistModel[] newArray(int size) {
            return new GistModel[size];
        }
    };

    public GistModel(String id, String url, String created, @NonNull String description,
                     String ownerLogin, String ownerAvatarUrl, @NonNull String note, boolean isLocal) {
        this.id = id;
        this.url = url;
        this.created = created;
        this.description = description;
        this.ownerLogin = ownerLogin;
        this.ownerAvatarUrl = ownerAvatarUrl;
        this.note = note;
        this.isLocal = isLocal;
    }

    public GistModel(GistModel other, @NonNull String desc, @NonNull String note) {
        id = other.id;
        url = other.url;
        created = other.created;
        ownerLogin = other.ownerLogin;
        ownerAvatarUrl = other.ownerAvatarUrl;
        isLocal = other.isLocal;

        this.description = desc;
        this.note = note;
    }

    private GistModel(Parcel in) {
        url = in.readString();
        id = in.readString();
        description = in.readString();
        created = in.readString();
        note = in.readString();
        ownerLogin = in.readString();
        ownerAvatarUrl = in.readString();
        isLocal = in.readInt() == 1;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(url);
        dest.writeString(id);
        dest.writeString(description);
        dest.writeString(created);
        dest.writeString(note);
        dest.writeString(ownerLogin);
        dest.writeString(ownerAvatarUrl);
        dest.writeInt(isLocal ? 1 : 0);
    }

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

    @Override
    public int type(TypesFactory factory) {
        return factory.type(this);
    }

    /**
     * различны ли элементы с точки зрения базы данных
     * @param other сравниваемый элемент
     * @return {@code true} - различны, иначе {@code false}
     */
    public boolean isDifferent(GistModel other) {
        return !other.id.equals(this.id);
    }

    public Uri insteadWebLink() {
        return Uri.parse(String.format("http://gist.github.com/%s/%s", ownerLogin, id));
    }

    @Override
    public String toString() {
        return "GistModel{" +
                "id='" + id + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 167 * result + url.hashCode();
        result = 167 * result + created.hashCode();
        result = 167 * result + description.hashCode();
        result = 167 * result + (ownerLogin != null ? ownerLogin.hashCode() : 0);
        result = 167 * result + (ownerAvatarUrl != null ? ownerAvatarUrl.hashCode() : 0);
        result = 167 * result + note.hashCode();
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        GistModel gistModel = (GistModel) o;

        if (!id.equals(gistModel.id)) return false;
        if (!url.equals(gistModel.url)) return false;
        if (!created.equals(gistModel.created)) return false;

        if (!description.equals(gistModel.description)) return false;

        if (ownerLogin != null) {
            if (!ownerLogin.equals(gistModel.ownerLogin)) return false;
        } else {
            if (gistModel.ownerLogin != null) return false;
        }

        if (ownerAvatarUrl != null) {
            if (!ownerAvatarUrl.equals(gistModel.ownerAvatarUrl)) return false;
        } else {
            if (gistModel.ownerAvatarUrl != null) return false;
        }

        return note.equals(gistModel.note);
    }

}

@@ -152,16 +152,60 @@
 NonNull;
+%0Aimport android.support.annotation.Nullable;
 %0A%0Aimport
@@ -530,24 +530,38 @@
 scription;%0A%0A
+    @Nullable%0A
     public f
@@ -581,24 +581,38 @@
 wnerLogin;%0A%0A
+    @Nullable%0A
     public f
@@ -1177,16 +1177,26 @@
         
+@Nullable 
 String o
@@ -1205,16 +1205,26 @@
 erLogin,
+ @Nullable
  String 
@@ -3042,126 +3042,358 @@
     
-public Uri insteadWebLink() %7B%0A        return Uri.parse(String.format(%22http://gist.github.com/%25s/%25s%22, ownerLogin, id));
+/**%0A     * %D1%84%D0%BE%D1%80%D0%BC%D0%B8%D1%80%D1%83%D0%B5%D1%82 %D1%81%D1%81%D1%8B%D0%BB%D0%BA%D1%83 %D0%BD%D0%B0 github.com%0A     * @return %D1%81%D1%81%D1%8B%D0%BB%D0%BA%D0%B0 %D0%BD%D0%B0 %D1%8D%D1%82%D0%BE%D1%82 %D0%B3%D0%B8%D1%81%D1%82%0A     */%0A    public Uri insteadWebLink() %7B%0A        if (ownerLogin != null) %7B%0A            return Uri.parse(String.format(%22http://gist.github.com/%25s/%25s%22, ownerLogin, id));%0A        %7D else %7B%0A            return Uri.parse(String.format(%22http://gist.github.com/%25s%22, id));%0A        %7D
 %0A   
@@ -3486,17 +3486,16 @@
         
- 
 %22id='%22 +
@@ -3499,33 +3499,32 @@
 %22 + id + '%5C'' +%0A
- 
                %22
@@ -3556,32 +3556,113 @@
 ption + '%5C'' +%0A 
+              %22, note='%22 + note + '%5C'' +%0A               %22, isLocal=%22 + isLocal +%0A
                '

improve GistModel