package cn.zhios.zhiying_base_widget_example;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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

import cn.zhios.zhiying_comm.ZhiyingFlutterCommHandler;
import cn.zhios.zhiying_comm.ZhiyingFlutterCommNativable;
import cn.zhios.zhiying_comm.ZhiyingFlutterCommNative;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterActivityLaunchConfigs;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.platform.PlatformPlugin;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity implements ZhiyingFlutterCommNativable {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        configureWindowForTransparency();
        configureStatusBarForFullscreenFlutterExperience();
    }

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        ZhiyingFlutterCommNative.getInstance().regist(this);
    }

    private void configureWindowForTransparency() {
        FlutterActivityLaunchConfigs.BackgroundMode backgroundMode = getBackgroundMode();
        if (backgroundMode == FlutterActivityLaunchConfigs.BackgroundMode.transparent) {
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            );
        }
    }

    private void configureStatusBarForFullscreenFlutterExperience() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.getDecorView().setSystemUiVisibility(PlatformPlugin.DEFAULT_SYSTEM_UI);
        }
    }


    boolean localDebug = false;

    @Override
    public void openPage(Map params) {

    }

    @Override
    public void openNativePage(Map params) {

    }

  @Override
  public Map getSetting() {
    Map map = new HashMap();
    map.put("domain", "http://inapi.izhyin.cn"); //"http://120.76.175.204:8989");
//    map.put("domain", "http://192.168.0.113:5000");
    map.put("master_id", "123456");
    map.put("secret_key", "123456");
//    map.put("token", "123465");
    return map;
  }

  @Override
  public void invokeMethod(String method, Map<String, Object> params, ZhiyingFlutterCommHandler handler) {
    return;
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    ZhiyingFlutterCommNative.getInstance().unregist();
  }
}
