package com.shoutcast.stm.radio_nome.fragments;

import static com.shoutcast.stm.radio_nome.utils.Constant.LOCALHOST_ADDRESS;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.shoutcast.stm.radio_nome.Config;
import com.shoutcast.stm.radio_nome.R;
import com.shoutcast.stm.radio_nome.adapters.AdapterSocial;
import com.shoutcast.stm.radio_nome.callbacks.CallbackConfig;
import com.shoutcast.stm.radio_nome.database.dao.AppDatabase;
import com.shoutcast.stm.radio_nome.database.dao.DAO;
import com.shoutcast.stm.radio_nome.database.dao.SocialEntity;
import com.shoutcast.stm.radio_nome.models.Social;
import com.shoutcast.stm.radio_nome.rests.ApiInterface;
import com.shoutcast.stm.radio_nome.rests.RestAdapter;
import com.shoutcast.stm.radio_nome.utils.Utils;
import com.solodroid.ads.sdk.util.Tools;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FragmentSocial extends DialogFragment {

    private RecyclerView recyclerView;
    private AdapterSocial adapterSocial;
    private SwipeRefreshLayout swipeRefreshLayout;
    private Call<CallbackConfig> callbackCall = null;
    ArrayList<Social> items = new ArrayList<>();
    DAO db;
    View rootView;
    RelativeLayout parentView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_social, container, false);
        initView();
        return rootView;
    }

    private void initView() {

        parentView = rootView.findViewById(R.id.parentView);
        parentView.setOnClickListener(v -> {
            //do nothing
        });

        (rootView.findViewById(R.id.button_close)).setOnClickListener(v -> {
            new Handler(Looper.getMainLooper()).postDelayed(() -> {
                if (getActivity() != null) {
                    FragmentManager fm = getActivity().getSupportFragmentManager();
                    if (fm.getBackStackEntryCount() > 0) {
                        fm.popBackStack();
                    }
                }
                Utils.darkStatusBar(getActivity(), true);
                dismiss();
            }, 300);
        });

        swipeRefreshLayout = rootView.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);

        recyclerView = rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));

        if (Config.ENABLE_REMOTE_JSON) {
            loadDataFromRemoteJson();
        } else {
            db = AppDatabase.getDb(getActivity()).get();
            loadDataFromDatabase(db.getAllSocial());
        }

        adapterSocial.setOnItemClickListener((view, obj, position) -> {
            //if (Config.OPEN_SOCIAL_MENU_IN_EXTERNAL_BROWSER) {
            if (obj.social_ext.contains("true")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(obj.social_url));
                startActivity(intent);
            } else {
                FragmentWebView fragmentWebView = new FragmentWebView();
                Bundle args = new Bundle();
                args.putString("title", obj.social_name);
                args.putString("url", obj.social_url);
                fragmentWebView.setArguments(args);

                FragmentManager fragmentManager = getFragmentManager();
                assert fragmentManager != null;
                FragmentTransaction transaction = fragmentManager.beginTransaction()
                        .setCustomAnimations(R.anim.slide_in, R.anim.fade_out, R.anim.fade_in, R.anim.slide_out);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                transaction.add(android.R.id.content, fragmentWebView).addToBackStack("page");
                transaction.commit();
            }
        });
    }

    private void loadDataFromDatabase(final List<SocialEntity> socials) {
        swipeRefreshLayout.setEnabled(false);
        adapterSocial = new AdapterSocial(getActivity(), new ArrayList<>());
        recyclerView.setAdapter(adapterSocial);
        ArrayList<Social> items = new ArrayList<>();
        for (SocialEntity p : socials) items.add(p.original());
        showNoItemView(false);
        showFailedView(false, "");
        adapterSocial.resetListData();
        adapterSocial.setItems(items);
        if (socials.size() == 0) {
            showNoItemView(true);
        }
    }

    private void loadDataFromRemoteJson() {
        adapterSocial = new AdapterSocial(getActivity(), items);
        recyclerView.setAdapter(adapterSocial);
        swipeRefreshLayout.setOnRefreshListener(() -> {
            if (callbackCall != null && callbackCall.isExecuted()) callbackCall.cancel();
            adapterSocial.resetListData();
            requestAction();
        });

        requestAction();
    }

    private void requestAPI(String remoteUrl) {
        ApiInterface apiInterface = RestAdapter.createAPI();
        if (remoteUrl.startsWith("http://") || remoteUrl.startsWith("https://")) {
            if (remoteUrl.contains("https://drive.google.com")) {
                String driveUrl = remoteUrl.replace("https://", "").replace("http://", "");
                List<String> data = Arrays.asList(driveUrl.split("/"));
                String googleDriveFileId = data.get(3);
                callbackCall = apiInterface.getDriveJsonFileId(googleDriveFileId);
            } else {
                callbackCall = apiInterface.getJsonUrl(remoteUrl);
            }
        } else {
            callbackCall = apiInterface.getDriveJsonFileId(remoteUrl);
        }
        callbackCall.enqueue(new Callback<CallbackConfig>() {
            @Override
            public void onResponse(@NonNull Call<CallbackConfig> call, @NonNull Response<CallbackConfig> response) {
                CallbackConfig resp = response.body();
                if (resp != null) {
                    displayData(resp.socials);
                } else {
                    onFailRequest();
                }
            }

            @Override
            public void onFailure(@NonNull Call<CallbackConfig> call, @NonNull Throwable t) {
                if (!call.isCanceled()) onFailRequest();
            }

        });
    }

    private void displayData(final ArrayList<Social> socials) {
        adapterSocial.setItems(socials);
        swipeProgress(false);
        if (socials.size() == 0) {
            showNoItemView(true);
        }
    }

    private void onFailRequest() {
        swipeProgress(false);
        if (Utils.isConnect(getActivity())) {
            showFailedView(true, getString(R.string.failed_text));
        } else {
            showFailedView(true, getString(R.string.failed_text));
        }
    }

    private void requestAction() {
        showFailedView(false, "");
        showNoItemView(false);
        swipeProgress(true);
        new Handler(Looper.getMainLooper()).postDelayed(()-> {
            String remoteUrl = Config.URL_JSON;
            requestAPI(remoteUrl);
        }, 10);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        swipeProgress(false);
        if (callbackCall != null && callbackCall.isExecuted()) {
            callbackCall.cancel();
        }
    }

    private void showFailedView(boolean show, String message) {
        View lyt_failed = rootView.findViewById(R.id.lyt_failed);
        ((TextView) rootView.findViewById(R.id.failed_message)).setText(message);
        if (show) {
            recyclerView.setVisibility(View.GONE);
            lyt_failed.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            lyt_failed.setVisibility(View.GONE);
        }
        rootView.findViewById(R.id.btn_failed_retry).setOnClickListener(view -> requestAction());
    }

    private void showNoItemView(boolean show) {
        View lyt_no_item = rootView.findViewById(R.id.lyt_no_item);
        ((TextView) rootView.findViewById(R.id.no_item_message)).setText(R.string.no_social_found);
        if (show) {
            recyclerView.setVisibility(View.GONE);
            lyt_no_item.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            lyt_no_item.setVisibility(View.GONE);
        }
    }

    private void swipeProgress(final boolean show) {
        if (!show) {
            swipeRefreshLayout.setRefreshing(false);
            return;
        }
        swipeRefreshLayout.post(() -> swipeRefreshLayout.setRefreshing(true));
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

}