package com.virjar.echo.adr.ui;

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.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.virjar.echo.adr.R;
import com.virjar.echo.adr.bean.CommonRes;
import com.virjar.echo.adr.component.LogServices;
import com.virjar.echo.adr.repo.APIServices;
import com.virjar.echo.adr.repo.EchoConfig;
import com.virjar.echo.nat.log.EchoLogger;

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

public class LogFragment extends Fragment {

    private TextView logView;

    private TextView mappingView;
    private boolean running = false;

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

        logView = view.findViewById(R.id.tv_log);
        logView.setText(LogServices.logSnapshot());

        mappingView = view.findViewById(R.id.tv_mapping_proxy);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateClientId();
    }

    private void updateClientId() {
        APIServices.userApi.getClientMappingPort(
                EchoConfig.getClientId()
        ).enqueue(new Callback<CommonRes<Integer>>() {
            @Override
            public void onResponse(Call<CommonRes<Integer>> call, Response<CommonRes<Integer>> response) {
                final CommonRes<Integer> commonRes = response.body();
                if (!commonRes.isOk()) {
                    new Handler(Looper.getMainLooper())
                            .postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    updateClientId();
                                }
                            }, 3000);
                    return;
                }


                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        mappingView.setText(EchoConfig.getEchoServer() + ":" + commonRes.getData());
                    }
                });
            }

            @Override
            public void onFailure(Call<CommonRes<Integer>> call, Throwable t) {
                EchoLogger.getLogger().error("can not get mapping info", t);
                new Handler(Looper.getMainLooper())
                        .postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                updateClientId();
                            }
                        }, 3000);
            }
        });
    }

    public void start() {
        if (running) {
            return;
        }
        running = true;
        updateLog();
    }

    public void stop() {
        running = false;
    }

    private void updateLog() {
        if (logView != null) {
            logView.setText(LogServices.logSnapshot());
        }

        if (!running) {
            return;
        }
        //updateClientId();
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                updateLog();
            }
        }, 500);
    }
}
