/*
 * Copyright (c) 2019 The sky Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.virjar.xposed.rimet.plugin.main;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.sky.xposed.common.ui.view.SimpleItemView;
import com.sky.xposed.common.util.Alog;
import com.sky.xposed.common.util.ResourceUtil;
import com.sky.xposed.javax.MethodHook;
import com.virjar.xposed.rimet.BuildConfig;
import com.virjar.xposed.rimet.Constant;
import com.virjar.xposed.rimet.data.M;
import com.virjar.xposed.rimet.data.model.PluginInfo;
import com.virjar.xposed.rimet.plugin.base.BasePlugin;
import com.virjar.xposed.rimet.plugin.interfaces.XPluginManager;

/**
 * Created by sky on 2018/12/30.
 */
public class SettingsPlugin extends BasePlugin {

    public SettingsPlugin(XPluginManager pluginManager) {
        super(pluginManager);
    }

    @Override
    public Info getInfo() {
        return new PluginInfo(Constant.Plugin.MAIN_SETTINGS, "设置");
    }

    @Override
    public void onHandleLoadPackage() {

        MethodHook.AfterCallback afterCallback = param -> {
            Alog.i("进入钉钉用户设置页面..");
            final Activity activity = (Activity) param.thisObject;

            View view = activity.findViewById(ResourceUtil.getId(activity, getXString(M.res.res_setting_msg_notice)));
            ViewGroup viewGroup = (ViewGroup) view.getParent();

            Alog.i("find msg notice view: " + view);
            final int index = viewGroup.indexOfChild(view);

            SimpleItemView viewDing = new SimpleItemView(activity);
            viewDing.getNameView().setTextSize(17);
            viewDing.setName(Constant.Name.TITLE);
            viewDing.setExtend("v" + BuildConfig.VERSION_NAME);
            viewDing.setOnClickListener(v -> {
                // 打开设置
                openSettings(activity);
            });
            viewGroup.addView(viewDing, index);
        };
        findMethod(
                M.classz.class_android_user_settings_activity_NewSettingActivity,
                M.method.method_android_user_settings_activity_NewSettingActivity_onCreate,
                Bundle.class)
                .after(afterCallback);
        findMethod(
                M.classz.class_android_user_settings_activity_UserSettingsActivity,
                M.method.method_android_user_settings_activity_UserSettingsActivity_onCreate,
                Bundle.class)
                .after(afterCallback);
    }

    @Override
    public void openSettings(Activity activity) {

        // 打开插件设置
        getPluginManager().getXPluginById(Constant.Plugin.DING_DING).openSettings(activity);

//        PluginSettingsDialog dialog = new PluginSettingsDialog();
//        dialog.show(activity.getFragmentManager(), "settings");
    }
}
