package com.huilan.updatelibrarysimple;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.huilan.updatelibrary.UpdateHelper;
import com.huilan.updatelibrary.UpdateEngineInterface;
import com.huilan.updatelibrary.UpdateStatus;
import com.huilan.updatelibrary.UpdateSuperBean;


public class DefaultActivity extends ActionBarActivity {
    private static String url = "http://bcs.duapp.com/huilan/version.json";
    private TextView text;
    private long startCurrentTime;
    private int time;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 200:
                    startMain();
                    break;
                case 201:
                    time += 100;
                    if (time > 10001)
                        return;
                    text.setText(String.valueOf(time / 1000f));
                    handler.sendEmptyMessageDelayed(201, 100);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.tv_text);
        startCurrentTime = System.currentTimeMillis();
        handler.sendEmptyMessage(201);

    }

    public void updateApp(View view) {
        UpdateHelper updateHelper = new UpdateHelper(this, url, null, new UpdateEngineInterface() {
            @Override
            public UpdateSuperBean parseBean(String response) {
                Log.i("response", response);
                VersionBean version = JsonUtil.parse2Bean(response, "version");
                return version;
            }
        }, new UpdateStatus() {
            @Override
            public void updateFail() {
                Toast.makeText(getApplicationContext(), "更新失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateNo() {
                Toast.makeText(getApplicationContext(), "已经是最新了", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateCancel(DialogInterface dialogInterface, int i) {
                Toast.makeText(getApplicationContext(), "取消更新", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateOther() {
                Toast.makeText(getApplicationContext(), "其他原因", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateConfirm(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "弹出对话框的操作", Toast.LENGTH_SHORT).show();
            }
        });
        updateHelper.showUpdate();
    }

    private void startMain() {
        long loadTime = System.currentTimeMillis() - startCurrentTime;
        if (loadTime < 10000) {
            handler.sendEmptyMessageDelayed(200, 10001 - loadTime);
            return;
        }
        handler.removeMessages(200);
        handler.removeMessages(201);
        startActivity(new Intent(getApplicationContext(), NextActivity.class));
    }

}
