package com.tencent.liteav.demo.superplayer.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.tencent.liteav.demo.superplayer.R;

public class PlayerRelativeLayout extends RelativeLayout {
    private int windowMarginLeft;
    private int windowMarginTop;
    private int windowMarginRight;
    private int windowMarginBottom;

    public PlayerRelativeLayout(Context context) {
        this(context, null);
    }

    public PlayerRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayerRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PlayerRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init(Context context, AttributeSet set, int defStyleAttr, int defStyleRes) {
        if (set != null) {
            TypedArray a = context.obtainStyledAttributes(set, R.styleable.PlayerRelativeLayout);
            windowMarginLeft = (int) a.getDimension( R.styleable.PlayerRelativeLayout_window_marginLeft, 0);
            windowMarginTop = (int) a.getDimension(R.styleable.PlayerRelativeLayout_window_marginTop, 0);
            windowMarginRight = (int) a.getDimension(R.styleable.PlayerRelativeLayout_window_marginRight, 0);
            windowMarginBottom = (int) a.getDimension(R.styleable.PlayerRelativeLayout_window_marginBottom, 0);
            a.recycle();
        }
    }

    public void onWindowMode() {
        setBackgroundResource(R.drawable.player_window_bg);

        MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams();
        layoutParams.setMargins(windowMarginLeft, windowMarginTop, windowMarginRight, windowMarginBottom);
        setLayoutParams(layoutParams);
    }

    public void onFullScreenMode() {
        setBackgroundResource(R.drawable.player_full_bg);

        MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams();
        layoutParams.setMargins(0, 0, 0, 0);
        setLayoutParams(layoutParams);
    }
}
