/*
 * Copyright (c) WhatsApp Inc. and its affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

package stickeradmin.signalstickers.trending.stickermaker.WhatsAppBasedCode;

import android.content.Context;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import stickeradmin.signalstickers.trending.BuildConfig;
import stickeradmin.signalstickers.trending.stickermaker.ImageManipulation;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class StickerPack implements Parcelable {
    Uri trayImageUri;
    String identifier;
    String name;
    String publisher;

    String IsAnimated;


    String SignalStickerPackURL;
    String trayImageFile;
    int stickerCount;
    boolean isRemove;

    public boolean isRemove() {
        return isRemove;
    }

    public void setRemove(boolean remove) {
        isRemove = remove;
    }

    public void setTrayImageUri(Uri trayImageUri, Context context) {
        this.trayImageFile = "trayimage";
        this.trayImageUri = ImageManipulation.convertIconTrayToWebP(trayImageUri, this.identifier, "trayImage", context);
    }

    int IsDownloaded;
    final String publisherEmail;
    final String publisherWebsite;
    final String privacyPolicyWebsite;
    final String licenseAgreementWebsite;

    String iosAppStoreLink;
    private List<Sticker> stickers;
    private long totalSize;
    String androidPlayStoreLink;
    private boolean isWhitelisted;
    private int stickersAddedIndex = 0;



//    public StickerPack(String identifier, String name, String publisher, Uri trayImageUri,int stickerCount , Context context) {
//        this.identifier = identifier;
//        this.name = name;
//        this.publisher = publisher;
//        this.trayImageFile = "trayimage";
//        this.trayImageUri = ImageManipulation.convertIconTrayToWebP(trayImageUri, this.identifier, "trayImage", context);
//        this.publisherEmail = "";
//        this.publisherWebsite = "";
//        this.privacyPolicyWebsite = "";
//        this.licenseAgreementWebsite = "";
//        this.stickerCount=stickerCount;
//        this.androidPlayStoreLink="https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID;
//        this.stickers = new ArrayList<>();
//        this.IsDownloaded=0;
//    }

//    public StickerPack(String identifier, String name, String publisher, int stickerCount , Context context) {
//        this.identifier = identifier;
//        this.name = name;
//        this.publisher = publisher;
//        this.trayImageFile = "trayimage";
//        //this.trayImageUri = ImageManipulation.convertIconTrayToWebP(trayImageUri, this.identifier, "trayImage", context);
//        this.publisherEmail = "";
//        this.publisherWebsite = "";
//        this.privacyPolicyWebsite = "";
//        this.licenseAgreementWebsite = "";
//        this.stickerCount=stickerCount;
//        this.androidPlayStoreLink="https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID;
//        this.stickers = new ArrayList<>();
//        this.IsDownloaded=0;
//    }

    public StickerPack(
                String identifier,
                String name,
                String publisher,
                Uri trayImageUri,
                String publisherEmail,
                String publisherWebsite,
                String privacyPolicyWebsite,
                String licenseAgreementWebsite,
                String SignalStickerPackURL ,
                String IsAnimated,
                Context context) {
        this.identifier = identifier;
        this.name = name;
        this.publisher = publisher;
        this.trayImageFile = "trayimage";
        this.trayImageUri = trayImageUri; //ImageManipulation.convertIconTrayToWebP(trayImageUri, this.identifier, "trayImage", context);
        this.publisherEmail = publisherEmail;
        this.publisherWebsite = publisherWebsite;
        this.privacyPolicyWebsite = privacyPolicyWebsite;
        this.licenseAgreementWebsite = licenseAgreementWebsite;
        this.androidPlayStoreLink="https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID;
        this.stickers = new ArrayList<>();
        this.IsDownloaded=0;
        this.IsAnimated=IsAnimated;
        this.SignalStickerPackURL=SignalStickerPackURL;
    }



    void setIsWhitelisted(boolean isWhitelisted) {
        this.isWhitelisted = isWhitelisted;
    }

    boolean getIsWhitelisted() {
        return isWhitelisted;
    }

    protected StickerPack(Parcel in) {
        identifier = in.readString();
        name = in.readString();
        publisher = in.readString();
        trayImageFile = in.readString();
        publisherEmail = in.readString();
        publisherWebsite = in.readString();
        privacyPolicyWebsite = in.readString();
        licenseAgreementWebsite = in.readString();
        iosAppStoreLink = in.readString();
        stickers = in.createTypedArrayList(Sticker.CREATOR);
        totalSize = in.readLong();
        androidPlayStoreLink = in.readString();
        isWhitelisted = in.readByte() != 0;
    }

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

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

    public void addStickerURI(Uri uri, Context context){
        String index = String.valueOf(stickersAddedIndex);
        this.stickers.add(new Sticker(
                index,
                uri,
                new ArrayList<String>()));
        stickersAddedIndex++;
    }

    public void addSticker(Uri uri, Context context){
        String index = String.valueOf(stickersAddedIndex);
        this.stickers.add(new Sticker(
                index,
              //  ImageManipulation.convertImageToWebP(uri, this.identifier, index, context),
                uri,
                new ArrayList<String>()));
        stickersAddedIndex++;
    }

    public void deleteSticker(Sticker sticker){
        new File(sticker.getUri().getPath()).delete();
        this.stickers.remove(sticker);
    }

    public Sticker getSticker(int index){
        return this.stickers.get(index);
    }

    public int getCount() {
        return this.stickers.size();
    }

    public Sticker getStickerById(int index){
        for(Sticker s : this.stickers){
            if(s.getImageFileName().equals(String.valueOf(index))){
                return s;
            }
        }
        return null;
    }

    public void setAndroidPlayStoreLink(String androidPlayStoreLink) {
        this.androidPlayStoreLink = androidPlayStoreLink;
    }

    public void setIosAppStoreLink(String iosAppStoreLink) {
        this.iosAppStoreLink = iosAppStoreLink;
    }

    public List<Sticker> getStickers() {
        return stickers;
    }

    public void blankStickerPack(){
        this.stickers=new ArrayList<>();

    }

    public long getTotalSize() {
        return totalSize;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(identifier);
        dest.writeString(name);
        dest.writeString(publisher);
        dest.writeString(trayImageFile);
        dest.writeString(publisherEmail);
        dest.writeString(publisherWebsite);
        dest.writeString(privacyPolicyWebsite);
        dest.writeString(licenseAgreementWebsite);
        dest.writeString(iosAppStoreLink);
        dest.writeTypedList(stickers);
        dest.writeLong(totalSize);
        dest.writeString(androidPlayStoreLink);
        dest.writeByte((byte) (isWhitelisted ? 1 : 0));
    }

    public String getName() {
        return name;
    }

    public int getIsDownloaded() {return IsDownloaded;}

    public void setIsDownloaded(int _IsDownloaded){ this.IsDownloaded=_IsDownloaded; }

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

    public String getIdentifier() {
        return this.identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public Uri getTrayImageUri() {
        return trayImageUri;
    }

    public void setStickers(List<Sticker> stickers) {
        this.stickers = stickers;
        totalSize = 0;
        for (Sticker sticker : stickers) {
            totalSize += sticker.size;
        }
    }

    public int getStickerCount() {
        return stickerCount;
    }

    public void setStickerCount(int stickerCount) {
        this.stickerCount = stickerCount;
    }

    public String getSignalStickerPackURL() {
        return SignalStickerPackURL;
    }

    public void setSignalStickerPackURL(String signalStickerPackURL) {
        SignalStickerPackURL = signalStickerPackURL;
    }

    public String getIsAnimated() {
        return IsAnimated;
    }

    public void setIsAnimated(String isAnimated) {
        IsAnimated = isAnimated;
    }

}
