package cn.zhios.zhiying_base_widget_example;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;

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

import com.example.moblink.MoblinkPlugin;
import com.mob.moblink.MobLink;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

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 {

    FrameLayout view;

    FrameLayout launchImageFrame;

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

        super.onCreate(savedInstanceState);
        configureWindowForTransparency();
        configureStatusBarForFullscreenFlutterExperience();
        MobLink.setRestoreSceneListener(new MoblinkPlugin.SceneListener());
        addLaunchView();

    }


    ///添加启动页
    void addLaunchView() {


        view = (FrameLayout) this.getWindow().getDecorView();

        launchImageFrame = new FrameLayout(this);

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

        params.gravity = Gravity.CENTER;

        launchImageFrame.setLayoutParams(params);


        ImageView launchImageView = new ImageView(this);

        FrameLayout.LayoutParams b_p = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

        b_p.gravity = Gravity.CENTER_HORIZONTAL;
        launchImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        launchImageView.setLayoutParams(b_p);
        launchImageView.setImageResource(R.mipmap.start);

        launchImageFrame.addView(launchImageView, b_p);

        view.addView(launchImageFrame, params);
    }

    Timer timer = new Timer();

    ///移除启动页
    void removeLaunchView() {
        //此处延时是为了不显示启动页往上顶

        if (view != null && launchImageFrame != null) {
            getWindow().getDecorView().setBackground(new ColorDrawable(Color.parseColor("#000000")));
//                Animation alphaAnimation = new AlphaAnimation(1, 1);
//                alphaAnimation.setDuration(100);
//                alphaAnimation.setFillAfter(true);
//                launchImageFrame.startAnimation(alphaAnimation);
            Log.d("----------------", "----------------------清除启动页---------------");
            view.removeView(launchImageFrame);
            launchImageFrame = null;
            view = null;
        }
    }


    @Override
    protected void onNewIntent(@NonNull Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        MobLink.updateNewIntent(getIntent(), this);
    }

    @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");http://47.114.51.254:80   http://inapi.izhyin.cn/
        map.put("master_id", "123456");
        map.put("secret_key", "123456");

        ///正式
//        map.put("domain", "http://api.zhios.cn/");//45678910，api.zhios.cn84198689
//        map.put("master_id", "99813608");
//        map.put("secret_key", "123456");

        //客户测试
//        map.put("domain", "http://api.zhiyingos.com/");//45678910，api.zhios.cn84198689
//        map.put("master_id", "13376168");
//        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
    public void initSuccess() {
        Log.d("----------------", "----------------------清除启动页---------------");
        removeLaunchView();
    }

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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.fontScale != 1) {
            getResources();
        }//非默认值
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
}
