package com.naijab.nextzytimeline.ui.people.detail.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.naijab.nextzytimeline.R;
import com.naijab.nextzytimeline.base.BaseMvpFragment;
import com.naijab.nextzytimeline.ui.people.editform.EditPeopleActivity;
import com.naijab.nextzytimeline.ui.people.manager.PeopleManager;
import com.naijab.nextzytimeline.ui.people.manager.PeopleModel;

import io.realm.Realm;

public class DetailPeopleFragment extends BaseMvpFragment<DetailPeopleFragmentInterface.Presenter>
        implements DetailPeopleFragmentInterface.View {

    private int id;
    private TextView nameAndLastName, job, dateBirth, dateJob, jobDescription, game, smartPhone;
    private ImageView profile, photo;
    private static final String ID = "id";
    // TODO Duplicated instance in DetailPeopleFragmentPresenter
    private Realm realm;

    public DetailPeopleFragment() {
        super();
    }

    public static DetailPeopleFragment newInstance(int id) {
        DetailPeopleFragment fragment = new DetailPeopleFragment();
        Bundle args = new Bundle();
        // TODO Why hardcode the String, what's about ID constant?
        args.putInt("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public DetailPeopleFragmentInterface.Presenter createPresenter() {
        return DetailPeopleFragmentPresenter.create();
    }

    @Override
    public int getLayoutView() {
        return R.layout.fragment_detail;
    }

    @Override
    public void bindView(View view) {
        nameAndLastName = (TextView) view.findViewById(R.id.tv_name);
        job = (TextView) view.findViewById(R.id.tv_job);
        dateBirth = (TextView) view.findViewById(R.id.tv_birthday);
        dateJob = (TextView) view.findViewById(R.id.tv_startjob);
        jobDescription = (TextView) view.findViewById(R.id.tv_job_description);
        game = (TextView) view.findViewById(R.id.tv_game);
        smartPhone = (TextView) view.findViewById(R.id.tv_smartphone);
        photo = (ImageView) view.findViewById(R.id.iv_photo);
        profile = (ImageView) view.findViewById(R.id.iv_profile);
    }

    @Override
    public void setupInstance() {
        setHasOptionsMenu(true);
    }

    @Override
    public void setupView() {
    }

    @Override
    public void initialize() {
        id = getArguments().getInt("id");
        getPeopleFromRealm(id);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_edit_person:
                Log.i("Detail", "onOptionsItemSelected: app edit");
                goToEditActivity();
                return true;
        }
        return false;
    }

    @Override
    public void restoreView(Bundle savedInstanceState) {
        // TODO Should be called in onRestoreInstanceState(Bundle savedInstanceState)
        /* TODO It would be better if you do like this
         * id = savedInstanceState.getInt("saveID", 0);
         * getPeopleFromRealm(id);
         *
         * TODO Say goodbye to useless "idSave" variable
         */
        int idSave = savedInstanceState.getInt("saveID", 0);
        id = idSave;
        getPeopleFromRealm(idSave);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // TODO Don't hardcode the String key
        outState.putInt("saveID", id);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    private void getPeopleFromRealm(int id) {
        PeopleModel people = PeopleManager.getInstance().getPeople(id);
        getPeopleByID(people);
    }

    public void getPeopleByID(PeopleModel people) {
        nameAndLastName.setText(people.getName());
        job.setText(people.getJob());
        dateBirth.setText(people.getBirthDay());
        dateJob.setText(people.getJobStart());
        jobDescription.setText(people.getJobDescription());
        game.setText(people.getGame());
        smartPhone.setText(people.getSmartPhone());
        setProfile(people.getProfile());
        setPhoto(people.getPhoto());
    }

    @Override
    public void updateRecycler() {
        getPeopleFromRealm(id);
    }

    private void setProfile(String urlProfile) {
        Glide.with(getActivity())
                .load(urlProfile)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .into(profile);
    }

    private void setPhoto(String urlPhoto) {
        Glide.with(getActivity())
                .load(urlPhoto)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .into(photo);
    }


    private void goToEditActivity() {
        Intent intent = new Intent(getActivity(), EditPeopleActivity.class);
        intent.putExtra(ID, id);
        startActivity(intent);
        getActivity().finish();
    }

}


@@ -1039,16 +1039,41 @@
  photo;%0A
+    private Realm realm;%0A
     priv
@@ -1098,16 +1098,23 @@
 tring ID
+_PEOPLE
  = %22id%22;
@@ -1122,92 +1122,54 @@
     
-// TODO Duplicated instance in DetailPeopleFragmentPresenter%0A    private Realm realm
+private static final String SAVE_ID = %22saveID%22
 ;%0A%0A 
@@ -1396,75 +1396,8 @@
 ();%0A
-        // TODO Why hardcode the String, what's about ID constant?%0A
     
@@ -1412,20 +1412,25 @@
 .putInt(
-%22id%22
+ID_PEOPLE
 , id);%0A 
@@ -2616,20 +2616,25 @@
 .getInt(
-%22id%22
+ID_PEOPLE
 );%0A     
@@ -3214,427 +3214,29 @@
     
-// TODO Should be called in onRestoreInstanceState(Bundle savedInstanceState)%0A        /* TODO It would be better if you do like this%0A         * id = savedInstanceState.getInt(%22saveID%22, 0);%0A         * getPeopleFromRealm(id);%0A         *%0A         * TODO Say goodbye to useless %22idSave%22 variable%0A         */%0A        int idSave = savedInstanceState.getInt(%22saveID%22, 0);%0A        id = idSave;%0A        getPeopleFromRealm(idSave
+getPeopleFromRealm(id
 );%0A 
@@ -3359,54 +3359,8 @@
 e);%0A
-        // TODO Don't hardcode the String key%0A
     
@@ -3379,24 +3379,23 @@
 .putInt(
-%22save
+SAVE_
 ID
-%22
 , id);%0A 
@@ -3536,24 +3536,105 @@
 anceState);%0A
+        int idSave = savedInstanceState.getInt(SAVE_ID, 0);%0A        id = idSave;%0A
     %7D%0A%0A    p
@@ -4936,24 +4936,24 @@
 ity.class);%0A
-
         inte
@@ -4966,16 +4966,23 @@
 Extra(ID
+_PEOPLE
 , id);%0A 

Add Save / Restore instance state