package io.github.kolacbb.translate.ui.activity;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.github.kolacbb.translate.R;
import io.github.kolacbb.translate.model.entity.YouDaoResult;
import io.github.kolacbb.translate.protocol.ApiKey;
import io.github.kolacbb.translate.protocol.ClientApi;
import io.github.kolacbb.translate.util.SpUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_point)
    TextView pointTextView;
    @BindView(R.id.answer)
    TextView answerTextView;

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

    @OnClick({R.id.tv_clear, R.id.bt_translate, R.id.add_book})
    public void widgetOnClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_clear:
                pointTextView.setText("");
                break;
            case R.id.bt_translate:
                //Toast.makeText(MainActivity.this, "translate", Toast.LENGTH_SHORT).show();
                if (pointTextView.getText().toString().trim().length() != 0)
                    queryTranslate(pointTextView.getText().toString());
                break;
            case R.id.add_book:
                String query = pointTextView.getText().toString().trim();
                String answer = answerTextView.getText().toString().trim();
                if (query.length() != 0 && answer.length() != 0) {

//                    String str = SpUtil.find("book");
//                    Gson gson = new Gson();
//                    Type type = new TypeToken<ArrayList<Book>>() {
//                    }.getType();
//                    List<Book> list = gson.fromJson(str, type);
//                    if (list == null )
//                        list = new ArrayList<>();
//                    Book book = new Book();
//                    book.setTitle(query);
//                    book.setDescription(answer);
//                    list.add(book);
//
//                    SpUtil.saveOrUpdate("book", gson.toJson(list));


                    Toast.makeText(MainActivity.this, "已加入生词本", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    public void queryTranslate(String text){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://fanyi.youdao.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClientApi clientApi = retrofit.create(ClientApi.class);

        Call<YouDaoResult> call = clientApi.getTranslationYouDao(ApiKey.YOUDAO_KEY_FROM,
                ApiKey.YOUDAO_KEY,
                ApiKey.YOUDAO_TYPE,
                ApiKey.YOUDAO_DOCTYPE,
                ApiKey.YOUDAO_VERSION,
                text);
        call.enqueue(new Callback<YouDaoResult>() {
            @Override
            public void onResponse(Call<YouDaoResult> call, Response<YouDaoResult> response) {
                answerTextView.setText(response.body().getTranslation().get(0));
            }

            @Override
            public void onFailure(Call<YouDaoResult> call, Throwable t) {

            }
        });
    }


}

@@ -2030,626 +2030,8 @@
  %7B%0A%0A
-//                    String str = SpUtil.find(%22book%22);%0A//                    Gson gson = new Gson();%0A//                    Type type = new TypeToken%3CArrayList%3CBook%3E%3E() %7B%0A//                    %7D.getType();%0A//                    List%3CBook%3E list = gson.fromJson(str, type);%0A//                    if (list == null )%0A//                        list = new ArrayList%3C%3E();%0A//                    Book book = new Book();%0A//                    book.setTitle(query);%0A//                    book.setDescription(answer);%0A//                    list.add(book);%0A//%0A//                    SpUtil.saveOrUpdate(%22book%22, gson.toJson(list));%0A%0A
 %0A   

test my pc connect with github