package com.app.simplitend.cg_subscription;

import static com.app.simplitend.apputils.AppUtil.NOTIFICATION_ACTION;
import static com.app.simplitend.apputils.Constants.MEDICINE_TIME;
import static com.app.simplitend.apputils.Constants.NEW_SUBSCRIPTION;
import static com.app.simplitend.apputils.NotificationService.CONTENT_TYPE_KEY;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.app.simplitend.BuildConfig;
import com.app.simplitend.R;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.CaregiverDataCache;
import com.app.simplitend.caregiverdashboard.activities.CgProfileProgressActivity;
import com.app.simplitend.databinding.ActivitySubscriptionWebviewBinding;

import java.util.HashMap;
import java.util.Map;

public class SubscriptionWebViewActivity extends AppCompatActivity {

    private static final String TAG = "aditya_sub_web_view";

    public static final String EXTRA_CAREGIVER_XID = "extra_caregiver_xid";

    protected ActivitySubscriptionWebviewBinding binding;

    private BroadcastReceiver paymentCompleteReceiver;

    private int caregiver_xid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySubscriptionWebviewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        caregiver_xid = getIntent().getIntExtra(EXTRA_CAREGIVER_XID, -1);

        paymentCompleteReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null){
                    String content_type = intent.getStringExtra(CONTENT_TYPE_KEY);
                    if (NEW_SUBSCRIPTION.equals(content_type)){
                        CaregiverDataCache.setCareGiverData(null);
                        CaregiverDataCache.getCaregiverData(SubscriptionWebViewActivity.this, careGiverData1 -> {
                            if (careGiverData1 != null && careGiverData1.isCaregiverTakeSubscription == 1){
                                setResult(RESULT_OK, null, null);
                                SubscriptionWebViewActivity.this.finish();
                            }
                        }, true);
                    }
                }
            }
        };

        registerReceiver(paymentCompleteReceiver, new IntentFilter(NOTIFICATION_ACTION));

        loadUrl();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(paymentCompleteReceiver);
    }

    private void loadUrl() {
        if (caregiver_xid == -1){
            Toast.makeText(this, "Something went wrong, please try again later", Toast.LENGTH_SHORT).show();
            return;
        }

        binding.progressBar.setVisibility(View.VISIBLE);

        binding.webView.getSettings().setJavaScriptEnabled(true);
        binding.webView.getSettings().setBuiltInZoomControls(true);
        binding.webView.getSettings().setDisplayZoomControls(false);

        binding.webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageCommitVisible(WebView view, String url) {
                super.onPageCommitVisible(view, url);
                binding.progressBar.setVisibility(View.GONE);
            }
        });

        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Bearer " + AppUtil.getCgToken(this));

        binding.webView.loadUrl(BuildConfig.SIMPLITEND_BASE_URL + "my-profile-ios?caregiver_xid="+caregiver_xid , headers);
    }
}