package com.bokecc.livemodule.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bokecc.livemodule.R;
import com.bokecc.livemodule.utils.TimeUtil;

/**
 * Copyright (C), 20105-2021, 创盛视联数码科技（北京）有限公司
 * FileName:
 * Author: cc视频
 * Date: 2024/4/26
 * Description:
 */
public class DLNAPlayView extends RelativeLayout {

    private TextView tvDlnaDeviceName;
    private TextView tvDlnaVideoName;
    private LinearLayout llDlnaReplayCon;
    private SeekBar progressDlnaReplay;
    private TextView tvDlnaCurrentTime;
    private TextView tvDlnaTotalTime;
    private ImageView ivDlnaLiveLabel;
    private LinearLayout llBtnExitDlna;
    private ImageView ivDlnaPlayPause;

    private boolean isPlaying = true;

    public DLNAPlayView(Context context) {
        super(context);

        initView();
    }

    public DLNAPlayView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initView();

    }

    public DLNAPlayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView();

    }



    private void initView(){
        View rootView = View.inflate(getContext(), R.layout.dlna_play_view, this);

        tvDlnaDeviceName = rootView.findViewById(R.id.tv_dlna_device_name);
        tvDlnaVideoName = rootView.findViewById(R.id.tv_dlna_video_name);
        llDlnaReplayCon = rootView.findViewById(R.id.ll_dlna_replay_con);
        progressDlnaReplay = rootView.findViewById(R.id.progress_dlna_replay);
        tvDlnaCurrentTime = rootView.findViewById(R.id.tv_dlna_current_time);
        tvDlnaTotalTime = rootView.findViewById(R.id.tv_dlna_total_time);
        ivDlnaLiveLabel = rootView.findViewById(R.id.iv_dlna_live_label);
        llBtnExitDlna = rootView.findViewById(R.id.ll_btn_exit_dlna);
        ivDlnaPlayPause = rootView.findViewById(R.id.iv_dlna_play_pause);


        setListener();

    }

    public void setOnPlayViewListener(OnPlayViewListener onPlayViewListener) {
        this.onPlayViewListener = onPlayViewListener;
    }

    private OnPlayViewListener onPlayViewListener;
    private boolean isTouchBarSelecting = false;
    public interface OnPlayViewListener{
        void onExit();
        void onPause();
        void onPlay();
        void onSeek(int position);
    }
    private void setListener() {
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        llBtnExitDlna.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onPlayViewListener!=null){
                    onPlayViewListener.onExit();
                }
            }
        });

        ivDlnaPlayPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                isPlaying = !isPlaying;
                if (onPlayViewListener!=null){
                    if (isPlaying){
                        onPlayViewListener.onPlay();
                        ivDlnaPlayPause.setImageResource(R.drawable.icon_dlna_pause);
                    }else {
                        onPlayViewListener.onPause();
                        ivDlnaPlayPause.setImageResource(R.drawable.icon_dlna_play);
                    }
                }

            }
        });
        progressDlnaReplay.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isTouchBarSelecting = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isTouchBarSelecting = false;
                if (onPlayViewListener!=null){
                    onPlayViewListener.onSeek(seekBar.getProgress());
                }
            }
        });

    }


    public void setIsLive(boolean isLive){
        if (isLive){
            llDlnaReplayCon.setVisibility(View.GONE);
            ivDlnaLiveLabel.setVisibility(View.VISIBLE);

        }else {
            llDlnaReplayCon.setVisibility(View.VISIBLE);
            ivDlnaLiveLabel.setVisibility(View.GONE);
        }
    }

    public void setDeviceName(String deviceName){
        tvDlnaDeviceName.setText(deviceName);
    }
    public void setVideoName(String videoName){
        tvDlnaVideoName.setText(videoName);
    }
    public void updateProgress(int progress,int max){
        //max s
        if (!isTouchBarSelecting){
            progressDlnaReplay.setMax(max);
            progressDlnaReplay.setProgress(progress);
            setCurrentTimeText(TimeUtil.getFormatTime(progress*1000));
            setTotalTimeText(TimeUtil.getFormatTime(max*1000));
        }

    }

    public void setCurrentTimeText(String currentTime){
        tvDlnaCurrentTime.setText(currentTime);
    }
    public void setTotalTimeText(String currentTime){
        tvDlnaTotalTime.setText(currentTime);
    }

}
