package com.genlot.ninglottery.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.genlot.ninglottery.R;
import com.genlot.ninglottery.toolkit.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author ZhuYongKang
 * @description TitleView
 * @date 2019/8/19
 */
public class TitleView extends RelativeLayout {
    private ScheduledThreadPoolExecutor executor;
    private SimpleDateFormat mDateFormat;
    private TextView tv_issueNumber,tv_titleTime, tv_titleDate, tvLeftTime;
    private TextView tvDi,tvQi;
    private LinearLayout ll_gameInfo;
    private Date mDate;
    private static final int UPDATE = 0;
    //开奖时间
    private long drawTime = 0;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == UPDATE) {
                Date dateTime = (Date) msg.obj;
                refreshDate(dateTime);
            }
        }
    };

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.view_title, null);
        addView(view);
        tv_issueNumber = view.findViewById(R.id.title_issue_number);
        tv_titleTime = view.findViewById(R.id.title_time);
        tv_titleDate = view.findViewById(R.id.title_date);
        ll_gameInfo = view.findViewById(R.id.title_game_info);
        tvLeftTime = view.findViewById(R.id.tv_left_time);
        tvDi = view.findViewById(R.id.text1);
        tvQi = view.findViewById(R.id.text2);
        ll_gameInfo.setVisibility(INVISIBLE);
        mDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault());
        mDate = new Date();
        refreshDate(mDate);
    }

    private void refreshDate(Date date) {
        String dateTime = mDateFormat.format(date);
        String day = dateTime.split(" ")[0];
        String time = dateTime.split(" ")[1];
        String week = TimeUtils.getWeek(date);
        tv_titleDate.setText(day);
        tv_titleTime.setText(String.format("%s %s", time, week));
        int lefTime = (int) ((drawTime - date.getTime() / 1000));
        String time1 = TimeUtils.getTime(lefTime);
        tvLeftTime.setText(time1);
    }

    public void startRefresh() {
        if (executor == null) {
            executor = new ScheduledThreadPoolExecutor(2);
        }
        executor.scheduleWithFixedDelay(() -> {
            long currentTime = System.currentTimeMillis();
            mDate.setTime(currentTime);
            Message message = Message.obtain();
            message.what = UPDATE;
            message.obj = mDate;
            handler.sendMessage(message);
        }, 1, 1, TimeUnit.SECONDS);
    }

    public void stopRefresh() {
        if (executor != null) {
            executor.shutdown();
            executor = null;
        }
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (visibility == VISIBLE) {
            startRefresh();
        } else {
            stopRefresh();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopRefresh();
    }

    public void setGameInfo(String issue, long time) {
        if (ll_gameInfo.getVisibility() != VISIBLE) {
            ll_gameInfo.setVisibility(VISIBLE);
        }
        tv_issueNumber.setText(issue);
        if (time == -1) {
            tvLeftTime.setVisibility(View.GONE);
        } else {
            drawTime = time;
            tvLeftTime.setVisibility(View.VISIBLE);
        }
    }

    public void showTrainMode() {
        if (ll_gameInfo.getVisibility() != VISIBLE) {
            ll_gameInfo.setVisibility(VISIBLE);
        }
        tv_issueNumber.setText("培训模式");
        tvDi.setVisibility(View.GONE);
        tvQi.setVisibility(View.GONE);
        tvLeftTime.setVisibility(View.GONE);
    }

}
