package ai.pai.lensman.main;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.common.utils.LogHelper;
import com.android.common.utils.NetworkUtil;
import com.android.views.loadingdrawable.LoadingView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import ai.pai.lensman.R;
import ai.pai.lensman.base.BaseActivity;
import ai.pai.lensman.bean.SessionBean;
import ai.pai.lensman.briefs.BriefsActivity;
import ai.pai.lensman.session.SessionActivity;
import ai.pai.lensman.upgrade.BoxUpgradeActivity;
import ai.pai.lensman.upload.UploadActivity;
import ai.pai.lensman.utils.UmengEvent;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements MainContract.View {

    @BindView(R.id.tv_box_status) TextView boxStatusTextView;
    @BindView(R.id.iv_box_status) ImageView boxStatusImageView;
    @BindView(R.id.icon_no_data)   View noDataLayout;
    @BindView(R.id.iv_add_session) ImageView addSessionBtn;
    @BindView(R.id.recycler_view_sessions) RecyclerView sessionsRecyclerView;
    @BindView(R.id.sync_time_view)   RelativeLayout syncTimeView;
    @BindView(R.id.loading_gear_view) LoadingView loadingView;
    @BindView(R.id.tv_sync_time_status) TextView syncStatusTextView;
    @BindView(R.id.btn_retry_sync) Button retrySyncBtn;
    private SessionRecyclerAdapter adapter;
    private MainContract.Presenter presenter;
    private long exitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);

        presenter = new MainPresenter(this);
        adapter = new SessionRecyclerAdapter(this);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL,false);
        sessionsRecyclerView.setLayoutManager(layoutManager);
        sessionsRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.start();
        LogHelper.d(TAG,"onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        presenter.stop();
        LogHelper.d(TAG,"onStop");
    }

    @OnClick(R.id.layout_box_wifi)
    void jump2WifiSettings(){
        try {
            Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
            startActivity(intent);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @OnClick(R.id.btn_retry_sync)
    void retrySync(){
        showTimeSyncView();
        presenter.resyncTime();
    }

    @OnClick(R.id.layout_brief)
    void jumpToBriefs(){
        MobclickAgent.onEvent(this, UmengEvent.home_brief_btn_click);
        startActivity(new Intent(this, BriefsActivity.class));
    }

    @OnClick(R.id.iv_upload_manage)
    void jumpToUploadManagePage(){
        startActivity(new Intent(this, UploadActivity.class));
    }

    @OnClick(R.id.iv_add_session)
    void jumpToNewSession(){
        LogHelper.d(TAG,"jumpToNewSession");
        MobclickAgent.onEvent(this, UmengEvent.home_add_session_btn_click);
        jumpToSelectedSession( presenter.createNewSession());
    }

    @Override
    public void showSnackBar(int stringId) {
        Toast.makeText(this,stringId,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showBoxDisconnectedView() {
        boxStatusTextView.setText(NetworkUtil.getWIFIName(this));
        boxStatusImageView.setImageResource(R.drawable.wifi_disconnect);
    }

    @Override
    public void showBoxConnectedView() {
        boxStatusTextView.setText(NetworkUtil.getWIFIName(this));
        boxStatusImageView.setImageResource(R.drawable.wifi_connect);
    }

    @Override
    public void showEmptyView() {
        syncTimeView.setVisibility(View.GONE);
        sessionsRecyclerView.setVisibility(View.GONE);
        noDataLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void showSessionViews() {
        noDataLayout.setVisibility(View.GONE);
        syncTimeView.setVisibility(View.GONE);
        sessionsRecyclerView.setVisibility(View.VISIBLE);
    }

    @Override
    public void updateSessionUploadViewAt(int position) {
        adapter.notifyItemChanged(position);
    }

    @Override
    public void refreshSessionViews(final ArrayList<SessionBean> sessionList) {
        adapter.clear();
        adapter.addSessionBeans(sessionList);
    }

    @Override
    public void setNewSessionBtnEnabled(boolean isEnabled) {
        addSessionBtn.setEnabled(isEnabled);
    }

    @Override
    public void showTimeSyncView() {
        sessionsRecyclerView.setVisibility(View.GONE);
        syncTimeView.setVisibility(View.VISIBLE);
        loadingView.setVisibility(View.VISIBLE);
        syncStatusTextView.setText(R.string.time_syncing);
    }

    @Override
    public void showRetrySyncView(int strId) {
        retrySyncBtn.setVisibility(View.VISIBLE);
        loadingView.setVisibility(View.INVISIBLE);
        if(strId!=0){
            syncStatusTextView.setText(strId);
        }
    }

    @Override
    public void showBoxUpgradeActivity(String bpversion_str) {
      BoxUpgradeActivity.startBoxUpgradeActivity(this,bpversion_str);
    }

    private void jumpToSelectedSession(SessionBean sessionBean) {
        Intent intent = new Intent(this, SessionActivity.class);
        intent.putExtra("session",sessionBean);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis()-exitTime<3000){
            super.onBackPressed();
        }else{
            Toast.makeText(this,R.string.app_exit_hint,Toast.LENGTH_SHORT).show();
        }
        exitTime = System.currentTimeMillis();
    }

}
