package cc.winboll.studio.contacts.fragments;

import android.os.Bundle;
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 cc.winboll.studio.contacts.R;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.LogView;

/**
 * @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
 * @Date 2025/02/20 12:58:15
 * @Describe 应用日志区域视图
 */
public class LogFragment extends Fragment {

    // ====================== 常量定义区 ======================
    public static final String TAG = "LogFragment";
    private static final String ARG_PAGE = "ARG_PAGE";

    // ====================== 页面参数区 ======================
    private int mPage;

    // ====================== UI控件区 ======================
    private LogView mLogView;

    // ====================== 实例化函数区 ======================
    public static LogFragment newInstance(int page) {
        LogUtils.d(TAG, "newInstance: 创建日志Fragment实例，页码=" + page);
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        LogFragment fragment = new LogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    // ====================== 生命周期函数区 ======================
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.d(TAG, "onCreate: Fragment创建开始");
        if (getArguments() != null) {
            mPage = getArguments().getInt(ARG_PAGE);
            LogUtils.d(TAG, "onCreate: 读取页面参数，mPage=" + mPage);
        }
        LogUtils.d(TAG, "onCreate: Fragment创建完成");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        LogUtils.d(TAG, "onCreateView: 加载Fragment布局");
        View view = inflater.inflate(R.layout.fragment_log, container, false);
        // Java7 适配：添加强制类型转换
        mLogView = (LogView) view.findViewById(R.id.logview);
        mLogView.start();
        LogUtils.d(TAG, "onCreateView: LogView已初始化并启动");
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.d(TAG, "onResume: Fragment进入前台");
        if (mLogView != null) {
            mLogView.start();
            LogUtils.d(TAG, "onResume: LogView已重启");
        } else {
            LogUtils.w(TAG, "onResume: LogView实例为空，跳过启动");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.d(TAG, "onDestroy: Fragment开始销毁");
        if (mLogView != null) {
            // 若LogView有停止方法，建议在此处调用，避免资源泄漏
            // mLogView.stop();
            LogUtils.d(TAG, "onDestroy: LogView资源已处理");
        }
        LogUtils.d(TAG, "onDestroy: Fragment销毁完成");
    }
}

