package com.danding.yang800;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.danding.yang800.Presenter.BindActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.View.AuthCodeCountDown;
import com.danding.yang800.View.NetworkProgressDialog;
import com.umeng.analytics.MobclickAgent;
import com.yanzhenjie.sofia.Sofia;

import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindPhoneActivity extends BaseActivity {
    @BindView(R.id.bindphone_backTv)
    TextView bindphone_backTv;
    @BindView(R.id.bindphone_NameEdit)
    EditText bindphone_NameEdit;
    @BindView(R.id.bindphone_CodeEdit)
    EditText bindphone_CodeEdit;
    @BindView(R.id.bindphone_codeTv)
    TextView bindphone_codeTv;
    @BindView(R.id.bindphone_codeNoTv)
    TextView bindphone_codeNoTv;
    @BindView(R.id.bindphone_button)
    TextView bindphone_button;
    @BindView(R.id.bindPhone_wxNickNameTv)
    TextView bindPhone_wxNickNameTv;
    private NetworkProgressDialog pd;
    BindActivityPresenter presenter;
    private PopupWindow popupWindow;
    SharedPreferences sp;
    String epCode = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_bind_phone);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        Intent intent = getIntent();
        String nickName = intent.getStringExtra("nickName");
        nickName="微信昵称";
        bindPhone_wxNickNameTv.setText(nickName);
        presenter = new BindActivityPresenter(this);
        if (sp.getString("epCode", "").equals("")) {
            epCode = UUID.randomUUID().toString();
            sp.edit().putString("epCode", epCode).commit();
        } else {
            epCode = sp.getString("epCode", "");
        }
    }

    @OnClick({R.id.bindphone_backTv, R.id.bindphone_NameEdit, R.id.bindphone_CodeEdit, R.id.bindphone_codeTv, R.id.bindphone_codeNoTv
            , R.id.bindphone_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bindphone_backTv:
                finish();
                break;
            case R.id.bindphone_codeTv:
                presenter.smsSend(bindphone_NameEdit.getText().toString().trim());
                break;
            case R.id.bindphone_codeNoTv:
                showWindow();
                break;
            case R.id.bindphone_button:
                String name = bindphone_NameEdit.getText().toString().trim();
                String code = bindphone_CodeEdit.getText().toString().trim();
                presenter.login(name, code, epCode);
//                startActivity(MainActivity.class);
//                BaseActivity.finishAll();
                break;

        }
    }

    public void showWindow() {
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.layout_popupwindows_smscode, null);
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        contentView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
    }

    public void initDialog() {
        if (pd == null) {
            pd = NetworkProgressDialog.createDialog(this);
            if (!pd.isShowing()) {
                pd.show();
            }
        }
    }

    public void dismissDialog() {
        if (pd != null) {
            pd.dismiss();
            pd = null;
            NetworkProgressDialog.clear();
        }
    }


    public void sendSuccess() {
        AuthCodeCountDown mAuthCodeDown = new AuthCodeCountDown(60 * 1000 + 1100, bindphone_codeTv);
        mAuthCodeDown.start();
        bindphone_codeTv.setEnabled(false);
    }

    public void loginSuccess() {
//        startActivity(PictureCheckActivity.class);
//        finish();
//        if (status.equals("99")) {
//            if (!isWorked("com.danding.yang800.Service.LoginKeepService")) {
//                Intent startIntent = new Intent(
//                        this,
//                        LoginKeepService.class);
//                startService(startIntent);
//            }
        sp.edit().putBoolean("login", true).commit();
        String cookie = sp.getString("Cookie", "") + ";S_INFO=" + (100000000 + sp.getInt("userId", 999999999))
                + ";D_INFO=" + (100000000 + sp.getInt("userId", 999999999));
        sp.edit().putString("Cookie", cookie).commit();
        MobclickAgent.onProfileSignIn(sp.getInt("userId", 999999999)+"");
        Intent intent = new Intent(BindPhoneActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
//        } else if (status.equals("3")) {
//            ToastUtil.show(this, "微信截图审核未通过");
//            startActivity(PictureCheckActivity.class);
//        } else if (status.equals("2")) {
//            ToastUtil.show(this, "微信截图审核中");
//            finish();
//        } else if (status.equals("1")) {
//            startActivity(PictureCheckActivity.class);
//            finish();
//        }
    }


    public void toMain() {
        MobclickAgent.onProfileSignIn(sp.getInt("userId", 999999999)+"");
        startActivity(MainActivity.class);
    }

    public void toGifitInfo(String storeSkuSn) {
        Intent intent = new Intent(BindPhoneActivity.this, GiftInfoActivity.class);
        intent.putExtra("storeSkuSn", storeSkuSn);
        startActivity(intent);
    }

    public void toGiftList() {
        startActivity(GiftListActivity.class);
    }
}
