package @APP_PACKAGE@.model;

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import android.database.Cursor;
import android.net.Uri;

import @APP_PACKAGE@.content.XBMCFileContentProvider;

import java.io.Serializable;

/*
 * Media class represents video entity with title, description, image thumbs and video url.
 *
 */
public class File implements Serializable
{

  public static final String NAME = "name";
  public static final String CATEGORY = "category";
  public static final String URI = "uri";
  public static final String MEDIATYPE = "mediatype";
  public static final String ID = "id";
  private static final String TAG = "File";
  private String name;
  private String category;
  private String mediatype;
  private long id;
  private String uri;

  private File()
  {
  }

  public File(String name, String category, String uri)
  {
    this.name = name;
    this.category = category;
    this.setUri(uri);

    this.mediatype = null;
    this.id = -1;
  }

  public static File createFile(String name, String category, String uri)
  {
    return new File(name, category, uri);
  }

  public static File fromCursor(Cursor cursor)
  {
    int index;
    File file = new File();

    if ((index = cursor.getColumnIndex(File.NAME)) >= 0 && !cursor.isNull(index))
      file.setName(cursor.getString(index));
    if ((index = cursor.getColumnIndex(File.CATEGORY)) >= 0 && !cursor.isNull(index))
      file.setCategory(cursor.getString(index));
    if ((index = cursor.getColumnIndex(File.URI)) >= 0 && !cursor.isNull(index))
      file.setUri(cursor.getString(index));
    if ((index = cursor.getColumnIndex(File.ID)) >= 0 && !cursor.isNull(index))
      file.setId(cursor.getLong(index));
    if ((index = cursor.getColumnIndex(File.MEDIATYPE)) >= 0 && !cursor.isNull(index))
      file.setMediatype(cursor.getString(index));

    return file;
  }

  @Override
  public String toString()
  {
    return "File{"
            + "id="
            + getId()
            + ", name='"
            + name
            + '\''
            + ", category='"
            + category
            + '\''
            + ", mediatype='"
            + mediatype
            + '\''
            + '}';
  }

  public String getName()
  {
    return name;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public String getCategory()
  {
    return category;
  }

  public void setCategory(String category)
  {
    this.category = category;
  }

  public String getMediatype()
  {
    return mediatype;
  }

  public void setMediatype(String mediatype)
  {
    this.mediatype = mediatype;
  }

  public long getId()
  {
    return id;
  }

  public void setId(long id)
  {
    this.id = id;
  }

  public String getUri()
  {
    return uri;
  }

  public void setUri(String uri)
  {
    this.uri = uri;
  }

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

    File file = (File) o;

    if (!name.equals(file.name)) return false;
    if (category != null ? !category.equals(file.category) : file.category != null) return false;
    return uri != null ? uri.equals(file.uri) : file.uri == null;
  }

  @Override
  public int hashCode()
  {
    int result = name.hashCode();
    result = 31 * result + (category != null ? category.hashCode() : 0);
    result = 31 * result + (uri != null ? uri.hashCode() : 0);
    return result;
  }
}
