package com.app.simplitend.caregiverdashboard.activities;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.CaregiverDataCache;
import com.app.simplitend.caregiverdashboard.activities.deactivateacc.AccountPresenter;
import com.app.simplitend.databinding.ActivityContactAdminBinding;
import com.app.simplitend.welcome.welcomecg.mvvm.CareGiverData;

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

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ContactAdminActivity extends AppCompatActivity implements AccountPresenter.ContactAdminCallback {

    private ActivityContactAdminBinding binding;

    private AccountPresenter presenter;
    private ProgressDialog progressDialog;

    private CareGiverData careGiverData;

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

        CaregiverDataCache.getCaregiverData(this, careGiverData -> {
            this.careGiverData = careGiverData;
        }, true);

        presenter = AccountPresenter.getAccountPresenter();
        progressDialog = new ProgressDialog(this);

        initViews();

        clickEvents();

    }

    @Override
    protected void attachBaseContext(Context newBase) {
        final Configuration configuration = new Configuration(newBase.getResources().getConfiguration());
        configuration.fontScale = Math.min(configuration.fontScale, 1.1f);
        applyOverrideConfiguration(configuration);
        super.attachBaseContext(newBase);
    }

    private void initViews() {

        smoothEditTextScroll();

    }

    @SuppressLint("ClickableViewAccessibility")
    private void smoothEditTextScroll() {
        // scrolling edit text

        binding.description.setOnTouchListener((v, event) -> {
            if (binding.description.hasFocus()) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_SCROLL) {
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    return true;
                }
            }
            return false;
        });

    }

    private void clickEvents() {
        binding.backBtn.setOnClickListener(v -> onBackPressed());

        binding.submit.setOnClickListener(v -> {
            if (careGiverData == null){
                Toast.makeText(this, "Could not connect.", Toast.LENGTH_SHORT).show();
                return;
            }

            if (allOkay()){
                submitResponse();
            }
        });
    }

    private void submitResponse() {
        progressDialog.setTitle("Please wait...");
        progressDialog.setMessage("while we submit your query");
        progressDialog.setCancelable(false);
        progressDialog.show();

        Map<String, RequestBody> body = new HashMap<>();

        RequestBody topic_body = RequestBody.create(binding.topic.getText().toString(), MediaType.parse("text/plain"));
        body.put("title", topic_body);

        RequestBody description_body = RequestBody.create(binding.description.getText().toString(), MediaType.parse("text/plain"));
        body.put("description", description_body);

        RequestBody query_body = RequestBody.create("None", MediaType.parse("text/plain"));
        body.put("query", query_body);

        RequestBody patient_xid_body = RequestBody.create(careGiverData.patientId + "", MediaType.parse("text/plain"));
        body.put("patient_xid", patient_xid_body);

        RequestBody caregiver_xid_body = RequestBody.create(careGiverData.caregiver_xid + "", MediaType.parse("text/plain"));
        body.put("caregiver_xid", caregiver_xid_body);

        presenter.contactAdmin(body,
                "Bearer " + AppUtil.getCgToken(this),
                this);

    }

    private boolean allOkay() {
        boolean allOkay = true;

        if (binding.topic.getText().toString().trim().isEmpty()){
            binding.topic.setError("Required");
            allOkay = false;
        }

        if (binding.description.getText().toString().trim().isEmpty()){
            binding.description.setError("Required");
            allOkay = false;
        }

        return allOkay;
    }

    @Override
    public void onSuccess() {
        progressDialog.dismiss();

        Toast.makeText(this, "Query sent successfully.", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void onFailed(String message) {
        progressDialog.dismiss();

        Toast.makeText(this, message + "", Toast.LENGTH_SHORT).show();
    }
}