package cc.winboll.studio.webpagesources.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
import cc.winboll.studio.webpagesources.R;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;

/**
 * @Author ZhanGSKen<zhangsken@qq.com>
 * @Date 2025/06/11 04:11
 * @Describe 网页状态信息栏
 */
public class StatusBarView extends LinearLayout {

    public static final String TAG = "StatusBar";

    public static final int MESSAGE_NEWS = 0; 

    Context mContext;
    TextView mtvMessage;

    public StatusBarView(Context context) {
        super(context);
        initView(context);
    }

    public StatusBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public StatusBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public StatusBarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    void initView(Context context) {
        mContext = context;
        View viewMain = inflate(context, R.layout.view_statusbar, null);
        mtvMessage = viewMain.findViewById(R.id.tv_message);

        // 设置布局参数为MATCH_PARENT，并确保最大尺寸显示
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        );
        addView(viewMain, params);
    }

    public void postMessage(String msg) {
        if (mContext != null) {
            Message message = new Message();
            message.what = MESSAGE_NEWS;
            message.obj = msg;
            mHandler.dispatchMessage(message);
        }
    }

    Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == MESSAGE_NEWS) {
                mtvMessage.setText((String)msg.obj);
            }
            //super.handleMessage(msg);
        }

    };
}
