package com.mnopensdk.demo.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;
import com.github.library.listener.OnRecyclerItemClickListener;
import com.mnopensdk.demo.base.BaseActivity;
import com.mnopensdk.demo.widget.PlayCloudAlarmDlg;
import com.mnopensdk.demo.utils.ToastUtils;
import com.mnopensdk.demo.utils.DateUtil;
import com.mnopensdk.demo.utils.LocalVariable;
import com.mnopensdk.demo.widget.LoadingDialog;
import com.mn.bean.restfull.AlarmTypeBean;
import com.mn.bean.restfull.AlarmsBean;
import com.mn.bean.restfull.CloudAlarmsBean;
import com.mn.bean.restfull.DevicesBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import MNSDK.MNKit;
import MNSDK.inface.MNKitInterface;
import butterknife.BindView;
import com.mnopensdk.demo.R;

/**
 * Created by Administrator on 2018/11/26 0026.
 */

public class CloudAlarmActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, OnRecyclerItemClickListener, LoadingDialog.OnTimerOutListener, MNKitInterface.CloudAlarmsCallBack {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refreshLay)
    SwipeRefreshLayout refreshLay;
    BaseRecyclerAdapter mAdapter;

    private List<AlarmsBean> mAlarms = new ArrayList<>();
    ArrayList<String> deviceSns = new ArrayList<>();
    ArrayList<AlarmTypeBean> types = new ArrayList<>();
    long startSearchTime, endSearchTime;

    PlayCloudAlarmDlg cloudAlarmDlg;
    DevicesBean mDevice;
    int mCurrentPage = 0;
    boolean noMoreLoad = false;
    boolean isRefresh = true;
    LoadingDialog loadingDialog;

    @Override
    protected int setViewLayout() {
        return R.layout.activity_cloud_alarm;
    }

    @Override
    protected void initData() {
        setTitle("云报警视频");
        mDevice = (DevicesBean) getIntent().getSerializableExtra("device_info");
        for (DevicesBean device : HomeActivity.devicesList) {
            deviceSns.add(device.getSn());
        }
        startSearchTime = LocalVariable.getDateBefore(new Date(), 31).getTime();
        endSearchTime = LocalVariable.getDateBeforeNight(new Date(), 0).getTime();

        AlarmTypeBean alarms = new AlarmTypeBean();
        alarms.setAlarmType(3);
        alarms.setSubAlarmType("3");
        types.add(alarms);

        AlarmTypeBean alarms1 = new AlarmTypeBean();
        alarms1.setAlarmType(3);
        alarms1.setSubAlarmType("4");
        types.add(alarms1);

        AlarmTypeBean alarms2 = new AlarmTypeBean();
        alarms2.setAlarmType(8);
        types.add(alarms2);

        AlarmTypeBean alarms3 = new AlarmTypeBean();
        alarms3.setAlarmType(10);
        types.add(alarms3);

        AlarmTypeBean alarms4 = new AlarmTypeBean();
        alarms4.setAlarmType(11);
        types.add(alarms4);

        AlarmTypeBean alarms5 = new AlarmTypeBean();
        alarms5.setAlarmType(12);
        alarms5.setSubAlarmType("1");
        types.add(alarms5);
    }

    @Override
    protected void initViews() {
        loadingDialog = new LoadingDialog(this).setReqTimeOutLintener(this);
        refreshLay.setColorSchemeResources(R.color.title_end, R.color.title_start);
        refreshLay.setOnRefreshListener(this);

        mAdapter = new BaseRecyclerAdapter<AlarmsBean>(this, mAlarms, R.layout.item_cloud_alarm) {
            @Override
            protected void convert(BaseViewHolder viewHolder, AlarmsBean data) {

                viewHolder.setText(R.id.tv_alarm_time, DateUtil.getStringDateByLong(data.getAlarmTime(), DateUtil.DEFAULT_M_D_FORMAT));
                viewHolder.setText(R.id.tv_alarm_type, "type : " + data.getAlarmType() + " , subType" + data.getSubAlarmType());
                viewHolder.setText(R.id.tv_dev_name, data.getPersonName());
                ImageView view = viewHolder.getView(R.id.iv_alarm_image);

                if (data.getVideoUrl() != null && !"".equals(data.getVideoUrl())) {
                    viewHolder.setText(R.id.tv_alarm_Image_type, "视频");
                } else if (data.getImageUrl() != null && !"".equals(data.getImageUrl())) {
                    viewHolder.setText(R.id.tv_alarm_Image_type, "图片");
                } else {
                    viewHolder.setText(R.id.tv_alarm_Image_type, "无效报警");
                }
                Glide.with(mContext).load(data.getImageUrl()).into(view);
            }
        };
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(mAdapter);
        mAdapter.openLoadAnimation(false);
        mAdapter.setOnRecyclerItemClickListener(this);

        recycler.setOnScrollListener(new RecyclerView.OnScrollListener() {
            boolean isSlidingToLast = false;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
                    int totlaItemCount = manager.getItemCount();
                    if (lastVisibleItem == (totlaItemCount - 1) && isSlidingToLast) {
                        if (!noMoreLoad) {
                            onLoadMore();
                        } else {
                            ToastUtils.MyToast("没有数据啦");
                        }
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    isSlidingToLast = true;
                } else {
                    isSlidingToLast = false;
                }
            }
        });

    }


    @Override
    protected void initEvents() {
        loadingDialog.show();
        MNKit.getCloudAlarmData(deviceSns, startSearchTime, endSearchTime, types, null, mCurrentPage, 20, this);
    }

    @Override
    protected void onBackKeyDown(boolean b) {
        finish();
    }

    @Override
    protected void onRightTvClick() {

    }

    @Override
    protected void onLeftTvClick() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (loadingDialog != null) {
            loadingDialog.onRelease();
        }
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 0;
        isRefresh = true;
        MNKit.getCloudAlarmData(deviceSns, startSearchTime, endSearchTime, types, null, mCurrentPage, 20, this);
    }

    private void onLoadMore() {
        mCurrentPage++;
        isRefresh = false;
        if (loadingDialog != null) {
            loadingDialog.show();
        }
        MNKit.getCloudAlarmData(deviceSns, startSearchTime, endSearchTime, types, null, mCurrentPage, 20, this);
    }

    @Override
    public void onGetCloudAlarmsFailed(String msg) {
        Log.e("CloudAlarmActivity", "onGetCloudAlarmsFailed : " + msg);
        if (!isRefresh) {
            mCurrentPage--;
        }
        refreshLay.setVisibility(View.VISIBLE);
        if (refreshLay.isRefreshing()) {
            refreshLay.setRefreshing(false);
        }
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onGetCloudAlarmsSuc(CloudAlarmsBean response) {
        // 下拉刷新
        if (refreshLay.isRefreshing()) {
            refreshLay.setRefreshing(false);
        }
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
        if (response == null || response.getAlarms() == null || response.getAlarms().size() == 0) {
            // TODO 显示没有数据界面
            if (isRefresh) {
                ToastUtils.MyToastCenter("没有报警数据");
            } else {
                noMoreLoad = true;
            }
        } else {
            // 有数据

            mAlarms.addAll(response.getAlarms());
        }
        mAdapter.setData(mAlarms);
    }

    @Override
    public void onItemClick(View view, int position) {
        AlarmsBean alarm = mAlarms.get(position);
        if (alarm.getVideoUrl() != null && !"".equals(alarm.getVideoUrl())) {
            Intent intent = new Intent(this, MNPlayControlActivity.class);
            intent.putExtra("cloudVideo", alarm);
            startActivity(intent);
        } else if (alarm.getImageUrl() != null && !"".equals(alarm.getImageUrl())) {
            ToastUtils.MyToastCenter("这是一个图片报警");
        } else {
            ToastUtils.MyToastCenter("这是一个无效报警");
        }
    }

    @Override
    public void OnReqTimerOut() {
        ToastUtils.MyToastBottom("请求超时");
    }
}
