package org.deke.risk.riskahead.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
import com.google.firebase.crashlytics.FirebaseCrashlytics;

import org.deke.risk.riskahead.R;
import org.deke.risk.riskahead.SubscriptionsActivity;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * (C) Copyright 2016 RiskAhead (http://www.riskahead.net)
 * Developer & Contributor: Dennis Thießen (dennis.thiessen@riskahead.net)
 * Date of Release: 10.02.2016
 *
 */
public class SubscriptionItemFragment extends Fragment {
    private final static String TAG = SubscriptionItemFragment.class.getSimpleName();

    private SubscriptionsActivity parent;
    private TextView txtTitle;
    private TextView txtDescription;
    private BootstrapButton btnPurchase;
    private ImageView imgSubs;
    private BootstrapButton btnBadge;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_subscriptionitem, container, false);
        parent = (SubscriptionsActivity)getActivity();

        txtTitle = view.findViewById(R.id.txt_subs_title);
        txtDescription = view.findViewById(R.id.txt_subs_desc);
        btnPurchase = view.findViewById(R.id.btn_sub_purchase);

        imgSubs = view.findViewById(R.id.img_subs);
        btnBadge = view.findViewById(R.id.btn_subs_membershiptype);

        txtTitle.setVisibility(View.GONE);
        txtDescription.setVisibility(View.GONE);
        btnPurchase.setVisibility(View.GONE);
        imgSubs.setVisibility(View.GONE);
        btnBadge.setVisibility(View.GONE);

        return view;
    }

    public void initSubscription(final JSONObject sku){

        try {
            final String id = sku.getString("productId");
            final String price = sku.getString("price");
            final String title = sku.getString("title");
            final String description = sku.getString("description");

            if(id.contains("no_ads")){
                imgSubs.setImageDrawable(getResources().getDrawable(R.drawable.noads));
                btnBadge.setBootstrapBrand(DefaultBootstrapBrand.PRIMARY);
                btnPurchase.setBootstrapBrand(DefaultBootstrapBrand.PRIMARY);
                btnBadge.setText(getResources().getString(R.string.lbl_membershiptype_addfree));
            }else if(id.contains("extra_function")){
                imgSubs.setImageDrawable(getResources().getDrawable(R.drawable.premium));
                btnBadge.setBootstrapBrand(DefaultBootstrapBrand.WARNING);
                btnPurchase.setBootstrapBrand(DefaultBootstrapBrand.WARNING);
                btnBadge.setText(getResources().getString(R.string.lbl_membershiptype_premium));
            }

            txtTitle.setText(title);
            txtDescription.setText(description);
            btnPurchase.setText(price);

            btnPurchase.setOnClickListener(v -> {
                Log.d(TAG, "PURCHASE: " + id);
                parent.buyProduct(id);
            });

            txtTitle.setVisibility(View.VISIBLE);
            txtDescription.setVisibility(View.VISIBLE);
            btnPurchase.setVisibility(View.VISIBLE);
            imgSubs.setVisibility(View.VISIBLE);
            btnBadge.setVisibility(View.VISIBLE);
        } catch (JSONException e) {
            FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
            crashlytics.log("JSONException caught: " + e);
        }
    }
}
