package com.zkjc.common.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.delegate.IActivity;
import com.jess.arms.integration.AppManager;
import com.jess.arms.integration.lifecycle.ActivityLifecycleable;
import com.jess.arms.mvp.IPresenter;
import com.xdja.watermarklibrary.WaterMarkUtils;

import javax.inject.Inject;

public abstract class ZkjcWithWMBaseActivity<P extends IPresenter> extends BaseActivity<P> implements IActivity, ActivityLifecycleable {

    @Inject
    AppManager mAppManager;
    protected SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if(null == sharedPreferences) sharedPreferences = getSharedPreferences("sys", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();

        String officeName = intent.getStringExtra("officeName");
        if (!TextUtils.isEmpty(officeName)) {
            edit.putString("officeName", officeName);
        }
        String userId = intent.getStringExtra("userId");
        if (!TextUtils.isEmpty(userId)) {
            edit.putString("userId", userId);
        }
        String jh = intent.getStringExtra("pcswhrjh");
        if (!TextUtils.isEmpty(jh)){
            edit.putString("jh",jh);
        }
        String photoGongzhang = intent.getStringExtra("photoGongzhang");
        if (!TextUtils.isEmpty(photoGongzhang)) {
            edit.putString("photoGongzhang", photoGongzhang);
        }
        String name = intent.getStringExtra("name");
        if (!TextUtils.isEmpty(name)) {
            edit.putString("name", name);
        }
        String phone = intent.getStringExtra("phone");
        if (!TextUtils.isEmpty(phone)) {
            edit.putString("phone", phone);
        }
        String officeId = intent.getStringExtra("officeId");
        if (!TextUtils.isEmpty(officeId)) {
            edit.putString("officeId", officeId);
        }
        String photo = intent.getStringExtra("photo");
        if (!TextUtils.isEmpty(photo)) {
            edit.putString("photo", photo);
        }
        String cardNo = intent.getStringExtra("cardNo");
        if (!TextUtils.isEmpty(cardNo)) {
            edit.putString("cardNo", cardNo);
        }


        String token = intent.getStringExtra("token");
        if (!TextUtils.isEmpty(token)) {
            edit.putString("token", token);
        }
        edit.commit();

        String prefix = "";
        if(TextUtils.isEmpty(sharedPreferences.getString("jh", "")))
            prefix = sharedPreferences.getString("cardNo", "") + sharedPreferences.getString("name", "");
        else
            prefix = sharedPreferences.getString("jh", "") + sharedPreferences.getString("name", "");
        String waterMark = "　　" + prefix;
        WaterMarkUtils.addWaterMark(this, waterMark, 350);
    }

    /*@Override
    protected void onStart() {
        super.onStart();
        if (useHermesEventBus())
            HermesEventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        if (useHermesEventBus())
            HermesEventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public boolean useHermesEventBus() {
        return false;
    }*/
    @Override
    public Resources getResources() {
        try {
            Context packageContext = createPackageContext("com.zkjc.policedemo", CONTEXT_IGNORE_SECURITY);
            SharedPreferences sp = packageContext.getSharedPreferences("FontSize", MODE_WORLD_READABLE);
            float font_size = sp.getFloat("font_size", 1.0f);
           // System.out.println("当前字体大小" + font_size);
            Resources res = super.getResources();
            Configuration config = res.getConfiguration();
            config.fontScale = font_size;//1 设置正常字体大小的倍数
            res.updateConfiguration(config, res.getDisplayMetrics());
            return res;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return super.getResources();
    }
}
