package com.emato.ich.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.emato.ich.MainActivity;
import com.emato.ich.R;
import com.emato.ich.api.ICSPClient;
import com.emato.ich.contant.SystemConfigConstant;
import com.emato.ich.databinding.FragmentTakeBinding;
import com.emato.ich.utils.ButtonUtils;
import com.emato.ich.utils.LoggingUtils;
import com.emato.ich.utils.StringUtils;
import com.emato.ich.utils.TimeOutUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 取件提示页面
 */
public class TakeFragment extends Fragment {

    private static final String TAG = TakeFragment.class.getName();
    private FragmentTakeBinding binding;
    private CountDownTimer timer;

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {

        binding = FragmentTakeBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//        ColorConstant.setButtonColor(binding.returnBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
        try {
            MainActivity activity = ((MainActivity) getActivity());
            timer = TimeOutUtils.timeout(activity, TakeFragment.this, binding.timeout, 60);
            timer.start();
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: 取件页面倒计时出现异常! ", e);
        }

        MainActivity activity = (MainActivity) getActivity();
        String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);

        if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {
            ICSPClient.getWeChatMPImage(url, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    Log.e(TAG, "onFailure: 请求微信公众号图片错误! 网络异常! ", e);
                    LoggingUtils.sendErrorLog("业务异常: 请求微信公众号图片错误! 网络异常!", e);
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    getActivity().runOnUiThread(() -> {
                        try {
                            InputStream is = response.body().byteStream();
                            final Drawable d = Drawable.createFromStream(is, "src");
                            binding.textView.setBackground(d);
                            is.close();
                        } catch (Exception e) {
                            Log.e(TAG, "onResponse: 设置图片异常! ", e);
                            LoggingUtils.sendErrorLog("业务异常: 设置图片异常! ", e);
                        }
                    });
                }
            });
        }

        // TODO 对接微信

        // 跳转输入取件码
        binding.toInputTakeCodeBtn.setOnClickListener( view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            timer.cancel();
            NavHostFragment.findNavController(TakeFragment.this)
                    .navigate(R.id.action_takeFragment_to_takeCodeFragment);
        });
        binding.returnBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            timer.cancel();
            NavHostFragment.findNavController(TakeFragment.this)
                    .navigate(R.id.action_takeFragment_to_mainFragment);
        });
    }


    @Override
    public void onDestroy() {
        timer.cancel();
        super.onDestroy();
        binding = null;
    }
}
